C++ - specialize function template on a templated class with a non type template parameter

Joald

I have a class template Foo:

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

And I have a function template validateType()

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

Now I want to specialize it for some types, including Foo, so that the function performs some static_asserts during compile time. I tried this:

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

and this:

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

In the first one, the compiler says:

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() {
                     ^

And in the second case I get

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

How should this be done?

yumetodo

Partial template specializations are not allowed for function templates.
Use SFINAE or class templates

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

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

Edit:

Is this supposed to work for template functions as well?

NO. Partial template specializations are not allowed for function templates.

for template function, use SFINAE.

For example, this sample check weather T is unsigned integer type(C++17).

template<typename T, std::enable_if_t<std::is_unsigned_v<T>, std::nullptr_t> = nullptr>
T foo(T n);

However, in your case, you chould use class templates. It's simplest way to use class templates to check wether T is template class foo(BTW, not for template class foo, std::is_same is simplest way).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can I specialize an variadic template template parameter with non template class?

From Dev

Can I specialize an variadic template template parameter with non template class?

From Dev

How to specialize a class template with a template template parameter?

From Dev

C++ class template with a non templated class?

From Dev

Specialize base class with template template parameter

From Dev

Specialize template with function pointer, that depends on template parameter

From Dev

C++ - specialize class template's member function

From Dev

Function template taking a template non-type template parameter

From Dev

Specialization template class with templated function

From Dev

Specialization template class with templated function

From Dev

How to specialize variadic template class for one type?

From Dev

How to specialize a C++ templated-class function basing on a type-dependent type?

From Dev

Specialize a template for void parameter

From Dev

Specialize function template with decltype trailing return type

From Dev

Is there a way to specialize a function template by an array type?

From Dev

Specialize function template with decltype trailing return type

From Dev

Calling template function with non-type template parameter using class attribute

From Dev

specialize a template class constructor

From Dev

specialize a template class constructor

From Dev

C++ templates pass in a templated class and specify the template type later

From Dev

C++ specialize single method in template class

From Dev

Specialize C++ class template with final specifier

From Dev

specialize template with inner class template of a class template

From Dev

Declaration of dependent type as function parameter in template class

From Dev

Passing a templated smart pointer type as template parameter

From Dev

how do I specialize a bound template friend function to a template class?

From Dev

non template function in a template class

From Dev

Wrapping each type in a variadic template in a templated class

From Dev

Function type not a valid type for a template non-type parameter?

Related Related

  1. 1

    Can I specialize an variadic template template parameter with non template class?

  2. 2

    Can I specialize an variadic template template parameter with non template class?

  3. 3

    How to specialize a class template with a template template parameter?

  4. 4

    C++ class template with a non templated class?

  5. 5

    Specialize base class with template template parameter

  6. 6

    Specialize template with function pointer, that depends on template parameter

  7. 7

    C++ - specialize class template's member function

  8. 8

    Function template taking a template non-type template parameter

  9. 9

    Specialization template class with templated function

  10. 10

    Specialization template class with templated function

  11. 11

    How to specialize variadic template class for one type?

  12. 12

    How to specialize a C++ templated-class function basing on a type-dependent type?

  13. 13

    Specialize a template for void parameter

  14. 14

    Specialize function template with decltype trailing return type

  15. 15

    Is there a way to specialize a function template by an array type?

  16. 16

    Specialize function template with decltype trailing return type

  17. 17

    Calling template function with non-type template parameter using class attribute

  18. 18

    specialize a template class constructor

  19. 19

    specialize a template class constructor

  20. 20

    C++ templates pass in a templated class and specify the template type later

  21. 21

    C++ specialize single method in template class

  22. 22

    Specialize C++ class template with final specifier

  23. 23

    specialize template with inner class template of a class template

  24. 24

    Declaration of dependent type as function parameter in template class

  25. 25

    Passing a templated smart pointer type as template parameter

  26. 26

    how do I specialize a bound template friend function to a template class?

  27. 27

    non template function in a template class

  28. 28

    Wrapping each type in a variadic template in a templated class

  29. 29

    Function type not a valid type for a template non-type parameter?

HotTag

Archive