C ++ template: no suitable function to call

Consider the following code

template <typename T, T one> T exponentiel(T val, unsigned n) { T result = one; unsigned i; for(i = 0; i < n; ++i) result = result * val; return result; } int main(void) { double d = exponentiel<double,1.0>(2.0f,3); cout << d << endl; return 0; } 

The compiler tells me that there is no corresponding function to call 'exponentiel (float, int)'

Why?

What is strange is that the exponentiel works with int.

+4
source share
1 answer

The problem is T one and 1.0 in the argument list of the template.

You cannot have a nontype template parameter for a floating point type, and you cannot pass a floating point value as a template argument. This is simply not allowed (as far as I know, there is really no good reason why this is not allowed).

Error message

g ++ is pretty useless here. Visual C ++ 2010 reports the following on the line where the template is used in main :

 error C2993: 'double' : illegal type for non-type template parameter 'one' 

Comeau Online Reports :

 line 13: error: expression must have integral or enum type double d = exponentiel<double,1.0>(2.0f,3); ^ line 2: error: floating-point template parameter is nonstandard template <typename T, T one> ^ 
+10
source

Source: https://habr.com/ru/post/1343263/


All Articles