samedi 30 janvier 2016

Knocking out a class template constructor depending on class template parameter

Given the following class template:

#include <type_traits>

template< class T, class Unrelated >
struct MyClass
{
    static_assert( std::is_same< T, char >::value ||
                   std::is_same< T, char16_t>::value ||
                   std::is_same< T, char32_t >::value,
                   "MyClass only defined for char, char16_t, char32_t" );
    MyClass( T init ) {}
    MyClass( char32_t init ) {}
    // ...
};

The second (char32_t) constructor is a special case for T == char and T == char16_t.

Obviously, it would generate an error for T == char32_t. So, I would like to "knock out" that constructor for that case. The class is rather large, with most of the code being shared for all T, so I would rather not specialize the whole class for the char32_t case.

I have seen enable_if as well as related answers like this one here on SO, but was unable to adapt any of the presented solutions / examples to my specific case (non-templated constructor in class template).

So I ask for your assistance:

How to disable the MyClass( char32_t ) constructor for MyClass< T, U > with T == char32_t?

Aucun commentaire:

Enregistrer un commentaire