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( ) {}
};
These helpers let you pass many types around as a bundle, a single argument.
Now your type
X
can look like:Users of
X
pass inX<T, types_tag<int, double, char>
.You can write adapters like:
where we make the
using
alias have a better name than the implementationstruct
.Bundles of types passed around as one type can make a whole bunch of metaprogramming simple. You can pass more than one bundle; and you can pass a
types_tag
by-value to a function as an argument to enable easy deduction of the contents of the bundle.Instead of
you may add a layer:
and then
For SFINAE, as default template parameter is not part of signature,
Should be rewritten, as for example: