2017-11-25 5 views
0

ich eine Klassenvorlage Foo haben:C++ - Funktionsvorlage auf einer Templat-Klasse mit einem nicht-Typ Template-Parameter spezialisiert

template <class A, A value, class B> 
class Foo {}; 

Und ich eine Funktionsschablone haben validateType()

template <class T> 
bool validateType() { 
    return false; 
} 

Now I Ich möchte es für einige Typen, einschließlich Foo, spezialisieren, so dass die Funktion einige static_asserts während der Kompilierzeit ausführt. Ich dies versucht:

template <class A, class B, Foo<A, A val, B>> 
bool validateType() { 
    // do some static asserts 
} 

und diese:

template <class A, A val, class B> 
bool validateType<Foo<A, val, B>>() { 
    // do some static asserts 
} 

In der ersten, der Compiler sagt:

error: wrong number of template arguments (2, should be 3) 
template <class A, class B, Foo<A, A val, B>> 
              ^~ 
note: provided for ‘template<class A, A value, class B> class Foo’ 
class Foo {}; 
     ^~~ 
error: two or more data types in declaration of ‘validateType’ 
bool validateType() { 
       ^
error: expected ‘>’ before ‘{’ token 
bool validateType() { 
        ^

Und im zweiten Fall, den ich bekommen

error: non-class, non-variable partial specialization ‘validateType<Foo<A, val, B> >’ is not allowed 
bool validateType<Foo<A, val, B>>() { 
           ^

Wie soll das gemacht werden?

Antwort

1

Partielle Vorlagenspezialisierungen sind für Funktionsvorlagen nicht zulässig.
Verwenden SFINAE oder Klassenvorlagen

template <class T> 
struct validateType : std::false_type {}; 

template <class A, A val, class B> 
struct validateType<Foo<A, val, B>> : std::true_type {}; 
Verwandte Themen