As I got a perfect answer for the question: Specializing class with SFINAE
For completeness I insert the correct solution as example here again:
class AA { public: using TRAIT = int; };
class BB { public: using TRAIT = float; };
template < typename T, typename UNUSED = void> class X;
template < typename T >
class X<T, typename std::enable_if< std::is_same< int, typename T::TRAIT>::value, void >::type>
{
public:
X() { std::cout << "First" << std::endl; }
};
template < typename T >
class X<T, typename std::enable_if< !std::is_same< int, typename T::TRAIT>::value, void >::type>
{
public:
X() { std::cout << "Second" << std::endl; }
};
int main()
{
X<AA> a;
X<BB> b;
}
But if I have to use a parameter pack for further use, I see no chance to write the things down like:
template < typename T, typename ...S, typename UNUSED = void> class X;
error: parameter pack 'S' must be at the end of the template parameter list
Having the definition in a different order like
template < typename T, typename UNUSED = void, typename ...S> class X;
ends up in problems if the first additional type is in use.
OK, what I describe is a technical solution which I can't find actually. Maybe there is a different one. What is my underlying problem: I need 2 different constructors for the class which call different base class constructors. But because both constructors have the same set of parameters I see no chance to specialize the constructors itself.
If specialize constructors can work, it can be something like that:
template < typename T>
class Y
{
public:
template <typename U = T, typename V= typename std::enable_if< std::is_same< int, typename U::TRAIT>::value, int >::type>
Y( const V* =nullptr) { std::cout << "First" << std::endl; }
template <typename U = T, typename V= typename std::enable_if< !std::is_same< int, typename U::TRAIT>::value, float >::type>
Y( const V* =nullptr) { std::cout << "Second" << std::endl; }
};
error: 'template template Y::Y(const V*)' cannot be overloaded
But as already mentioned... I have no idea if that can be done.
To show the underlying problem, I would give the following example which shows the different use of base class constructors dependent on a trait which is defined in the base class.
template <typename T, typename ... S>: public T
class Z
{
public:
// should work if T defines a trait
Z( typename T::SomeType t): T( t ) {}
// should be used if T defines another trait
Z( typename T::SomeType t): T( ) {}
};