C++ and derived classes issue

John Ralls jralls at ceridwen.us
Sat Feb 13 16:14:37 EST 2016


> On Feb 13, 2016, at 12:25 PM, Larry Evans <cppljevans at suddenlink.net> wrote:
> 
> 
> 
> 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.
> 

Nice. I haven't seen that use of 'typename' before, but I'm still a beginner at metaprogramming. What does it actually do there? IOW, what's the difference between 'using T = PropIndex2PropType::Type' and 'using T = typename PropIndex2PropType::Type'?

Using boost::mpl::vector could also replace the abstract superclass at the expense of having to explicitly list all of the GncTransPropImpl specializations in its declaration. 

Aside: I'm a bit leery of using too much metaprogramming in GnuCash. While the gurus (Sutter, Myers, et. al.) have been promoting it for the last several years it seems to be pretty esoteric still and it might be too incomprehensible to most potential contributors.

Regards,
John Ralls




More information about the gnucash-devel mailing list