user1496542
user1496542

Reputation: 559

No Matching function call to templated function with const parameters

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.

Upvotes: 4

Views: 753

Answers (4)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361642

When you call like this:

doSomething(12.0, 23.0f, 2.0f);

then you basically let the compiler to deduce the template argument T, from the function arguments. But in the above call, type of all arguments are not same : the first argument12.0 is double, while the rest two arguments are float (Note that f in 2.0f makes it a float, while 2.0 is a double.). Hence the problem, as the compiler is trying to find a function which accepts double, float, float as arguments. But there is no such function. And the function template cannot be deduced from the arguments, because T has to be same for all arguments.

So the solution is, let T be deduced as either double or float:

doSomething(12.0f, 23.0f, 2.0f);  //all are float now

Or this:

doSomething(12.0, 23.0, 2.0);    //all are double now

In the first case, T is deduced as float, and in the second case, T is deduced as double.

Alternately, you may choose not to depend on template argument deduction, by providing template argument as:

doSomething<float>(12.0, 23.0f, 2.0f); 

Or this:

doSomething<double>(12.0, 23.0f, 2.0f); 

Here, in the first case, the 1st argument converts into float, and in the second case, 2nd and 3rd argument convert into double.

Upvotes: 0

Oleg Tsarev
Oleg Tsarev

Reputation: 146

Your function definition uses same type "T" for every of three arguments. C++ is not able to deduct types in cases like this.

Please choose way to fix:

  • Different types for every argument
    template<typename A, typename B, typename C>
    A doSomething(const A& value, const B& value2, const C& value3)
    {
        A temp = value;
        //Do stuff
        return temp ;
    }
  • Explicit template argument on call:
    doSomething<int>(12.0, 23.0f, 2.0f);
  • Explicit type cast for argument on call:
    doSomething(12.0, static_cast<double>(23.0f), static_cast<double>(2.0f));

Upvotes: 6

KRyan
KRyan

Reputation: 7608

When using a templated function, you need to supply the template parameters within angle brackets.

I.e. you need to use doSomething<float>(12.0, 23.0f, 2.0f);

Or, at least, if you don't, the compiler has to guess what T is, which means when it sees a double for the first argument it assumes you mean doSomething<double>()

Upvotes: 0

Bo Persson
Bo Persson

Reputation: 92331

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.

Upvotes: 8

Related Questions