Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ template parameter in array dimension

Tags:

c++

templates

I have have the following code using templates and array dimension as template non-type parameter

template<int n> double f(double c[n]);
...
double c[5];
f<5>(c);  // compiles
f(c);  // does not compile

should not the compiler to be able to instantiate the second f without explicit template parameter? I am using g++4.1

like image 739
Anycorn Avatar asked Nov 17 '09 00:11

Anycorn


People also ask

Can we pass Nontype parameters to templates?

Template classes and functions can make use of another kind of template parameter known as a non-type parameter. A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument.

Which template can have multiple parameters?

C++ Templates: Templates with Multiple Parameters | C++ Tutorials for Beginners #65.

Can a template be a template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

Why do we use template template parameter?

8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.


1 Answers

It works when using references:

template<size_t n> double f(double (&c)[n]);
like image 146
Georg Fritzsche Avatar answered Oct 23 '22 05:10

Georg Fritzsche