Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template specialization works with int and fails with double as a class

Tags:

c++

templates

I hava a following simple template:

template<class T, T N>
bool VerifyGT(T value) {
  return value > N;
}

bool (*test1)(int) = &VerifyGE< int, (int) 0>;  // (1)
bool (*test2)(double) = &VerifyGE< double, (double) 0.0>;  // (2)

When compiling: test1 initialization succeeds, test2 fails with "doesn not match required type". Any ideas?

like image 341
Paweł Szczur Avatar asked Jun 17 '13 14:06

Paweł Szczur


People also ask

What is meant by template specialization Mcq?

Explanation: Template specialization is used when a different and specific implementation is to be used for a specific data type. In this program, We are using integer and character.

What is a template specialization in C++?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

Are template specializations inline?

An explicit specialization of a function template is inline only if it is declared with the inline specifier (or defined as deleted), it doesn't matter if the primary template is inline.

What is the syntax for explicit class specialization?

What is the syntax to use explicit class specialization? Explanation: The class specialization is creation of explicit specialization of a generic class. We have to use template<> constructor for this to work. It works in the same way as with explicit function specialization.


1 Answers

Non-type template arguments cannot be of floating-point type. Only integral types, enumerations, pointers and references are allowed.

like image 140
Angew is no longer proud of SO Avatar answered Sep 20 '22 08:09

Angew is no longer proud of SO