Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Matching function call to templated function with const parameters

Tags:

c++

gcc

I have a function that is defined as follows:

template < class T> T doSomething(const T value, const T value2, const T value3)
{
   T temp = value;
       //Do stuff
   return temp ;
}

in my main, I go to call it as follows:

doSomething(12.0, 23.0f, 2.0f);

I get an error saying no matching function for call doSomething(double, float, float).

I tried to used const_cast but it didn't seem to fix the issue. Any help would be appreciated.

like image 941
user1496542 Avatar asked Dec 08 '22 22:12

user1496542


1 Answers

It's not about the const, but about that T cannot be both double and float at the same time.

If you have a non-template function, one or more parameter can be promoted (or converted) to the parameter type. With a template function, the compiler will have to start by determining what the template type is supposed to be. Here it cannot decide.

like image 50
Bo Persson Avatar answered Dec 28 '22 22:12

Bo Persson