lundi 4 juillet 2016

C++ metafunction to retrieve constexpr from templated base classes


I have code that looks something like this:

template <typename Tag, int v>
struct Const
{
    static constexpr int value = v;
};


struct Compound
    :
    Const<struct T1, 111>,
    Const<struct T2, 222>
{
    Compound(); // constructor is not constexpr
};

I would like to know a compile-time construct (e.g. metafunction) that given either struct T1 or struct T2 as argument can extract as constexpr int 111 or 222 respectively from Compound.

I think I want a metafunction that looks something like this but don't know how to write it:

template <class C, typename Tag>
struct GetConst
{
    static constexpr int value = /* some magic */
};

GetConst<Compound, struct T1>::value == 111
GetConst<Compound, struct T2>::value == 222

Any ideas?


Aucun commentaire:

Enregistrer un commentaire