dimanche 8 mars 2015

enable class's member depending on template

I already know that you can enable (or not) a class's method using std::enable_if


for exemple:



template<size_t D, size_t E>
class Field
{
...

size_t offsetof(const std::array<float,D>& p) const
{
...
}

template<typename TT = size_t>
typename std::enable_if<D!=E, TT>::type
offsetof(const std::array<float,E>& p) const
{
return offsetof(_projection(p));
}

...
};


This helps not being able to call function that are invalid in a specific case as well as removing overloading errors ... which, to me, is very nice !


I'd like to go further and make some of my class's members being present only if the are needed. That way I would get an error if I try to use an objected which would have otherwise not been initiated


I tried to do



template<size_t D, size_t E>
class Field
{
...

template<typename TT = projectionFunc>
typename std::enable_if<D!=E, TT>::type _projection;
}


But the compiler tells me :



erreur: data member ‘_projection’ cannot be a member template


Is there any way to achieve what I want ?


Aucun commentaire:

Enregistrer un commentaire