mardi 28 avril 2020

Name (or refer) a specialized template parameter

I decided to use the following pattern to combine various types of objects and their configs automatically:

enum class Type { Car, Person };

template< Type TYPE >
struct Object;

template< Type TYPE >
struct ObjectConfig;

template<>
struct ObjectConfig< Type::Car >
{
};

// Version 1
template<>
struct Object< Type::Car >: public ObjectConfig< Type::Car > // How could I avoid this duplication???
{
};

// Version 2
template<>
struct Object< Type::Car >
{
    static constexpr Type myType{ Type::Car }; // How could I avoid this duplication???
    ObjectConfig< myType > m_params;
};

It is intended to be error-proof and automatic, but I can not avoid writing the enum value twice (which is error prone and non-automatic at all). I would like to write something similar (so I would like to refer the specialized parameter somehow):

template< Type TYPE >
struct Object;

template<>
struct Object< Type::Car >: public ObjectConfig< TYPE >
{
};

Is there any trick I could use to achieve something similar?

Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire