C++ and derived classes issue
Larry Evans
cppljevans at suddenlink.net
Sat Feb 13 15:25:17 EST 2016
On 02/13/2016 01:03 PM, John Ralls wrote:
[snip]
> You started out trying to encapsulate an object and a flag that
indicated where to put it. That had genericity problems because there's
no type-safe way to generically handle the differing return types.
Polymorphism to the rescue! Except that it turns out that you have
different handling requirements depending not on the type that you're
storing but on the actual property that you're processing.
>
> How about this:
> template <typename T, GncTransPropType p> class GncTransPropImpl {
> public:
> GncTransPropImpl (T v) : value {v} {}
> ...
> friend template<>void
set_property(std::unique_ptr<GncTransPropImpl<T, P>> prop, Transaction
*txn);
> friend template<>void
set_property(std::unique_ptr<GncTransPropImpl<T, P>> prop, Split *split);
> private:
> T value;
> }
Why not make T a metafunction of p? IOW, define:
template<GncTransPropType Index>
struct PropIndex2PropType
;
template<>
struct PropIndex2PropType<ACCOUNT>
{
using Type = Account*;
};
template <GncTransPropType p> class GncTransPropImpl {
public:
using T = typename PropIndexPropType::type;
GncTransPropImpl (T v) : value {v} {}
...
private:
T value;
};
That way one template argument is avoided.
You could also just define a typelist using
maybe boost::mpl::vector<T0,T1,T2,...>
and index into that instead of using PropIndex2PropType.
-regards,
Larry
More information about the gnucash-devel
mailing list