Why is a class/struct declaration with different number of template parameters not allowed?

rmi
template <class T1, class T2>
class A {};

template <class T1>
class A {};

A<int, int> a;
A<int> b;

This code generates

error C2976: 'A' : too few template arguments

at the second diclaration of 'A' class.

Shoe

Your first declaration defines a class A with 2 template arguments. Anything after that must either be a specialization of that or some other enabled version.

If you want to allow either 1 or 2 template arguments you can use variadic templates as follows:

template <class... Args>
class A;

template <class T1, class T2>
class A<T1, T2> {};

template <class T1>
class A<T1> {};

Live demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is a class/struct declaration with different number of template parameters not allowed?

From Dev

Function template specialization with overloads with different number of parameters

From Dev

Why is a forward declaration in a function declaration allowed?

From Dev

Declaration and declaration with definition. Why is this not allowed?

From Dev

why it is allowed to pass insufficient number of parameters when calling a function in C?

From Dev

Why is variable declaration not allowed here?

From Dev

Constexpr is not allowed in declaration of friend template specialization?

From Dev

Why are type parameters are not allowed on this type?

From Dev

Why is ;; allowed after a local variable declaration, but not after a field declaration?

From Dev

Template with variable number of parameters

From Dev

When are fold expressions as template parameters allowed?

From Java

Maximum number of parameters in Java method declaration

From Dev

creating template function with different parameters

From Dev

Why sealed classes are not allowed to be generic type parameters?

From Dev

Why sealed classes are not allowed to be generic type parameters?

From Dev

Why is this declaration of a function in template class invalid?

From Dev

Why is the following template declaration ill-formed?

From Dev

why Specialized template class need forward declaration?

From Dev

Why isn't short variable declaration allowed at package level in Go?

From Dev

Fetching entities with different number of parameters

From Dev

Calling functions with different number of parameters

From Dev

Template template parameters, why is class forced?

From Dev

Why is a space allowed between minus and a number?

From Dev

Partial specialization of template template parameters, with varying number of parameters

From Dev

variable declaration not allowed here

From Dev

Variable declaration not allowed in Java

From Dev

variable declaration not allowed here

From Dev

Java variable declaration not allowed

From Dev

why declaration of methods in different style different results in scala

Related Related

  1. 1

    Why is a class/struct declaration with different number of template parameters not allowed?

  2. 2

    Function template specialization with overloads with different number of parameters

  3. 3

    Why is a forward declaration in a function declaration allowed?

  4. 4

    Declaration and declaration with definition. Why is this not allowed?

  5. 5

    why it is allowed to pass insufficient number of parameters when calling a function in C?

  6. 6

    Why is variable declaration not allowed here?

  7. 7

    Constexpr is not allowed in declaration of friend template specialization?

  8. 8

    Why are type parameters are not allowed on this type?

  9. 9

    Why is ;; allowed after a local variable declaration, but not after a field declaration?

  10. 10

    Template with variable number of parameters

  11. 11

    When are fold expressions as template parameters allowed?

  12. 12

    Maximum number of parameters in Java method declaration

  13. 13

    creating template function with different parameters

  14. 14

    Why sealed classes are not allowed to be generic type parameters?

  15. 15

    Why sealed classes are not allowed to be generic type parameters?

  16. 16

    Why is this declaration of a function in template class invalid?

  17. 17

    Why is the following template declaration ill-formed?

  18. 18

    why Specialized template class need forward declaration?

  19. 19

    Why isn't short variable declaration allowed at package level in Go?

  20. 20

    Fetching entities with different number of parameters

  21. 21

    Calling functions with different number of parameters

  22. 22

    Template template parameters, why is class forced?

  23. 23

    Why is a space allowed between minus and a number?

  24. 24

    Partial specialization of template template parameters, with varying number of parameters

  25. 25

    variable declaration not allowed here

  26. 26

    Variable declaration not allowed in Java

  27. 27

    variable declaration not allowed here

  28. 28

    Java variable declaration not allowed

  29. 29

    why declaration of methods in different style different results in scala

HotTag

Archive