I wonder how to solve a problem that I have with a template function. The function definition looks like
but this produces an error when I'm trying to compile a program with the codeCode:template<typename T> T foo(T a, T b) { return a + b; }
The error message readsCode:foo(0, 0.5)
I tried to fix this by adding two more functions, so I hadCode:error: no matching function for call to 'foo(int, double)'
which solved the first compile error, but then the lineCode:template<typename T> T foo(T a, T b) { return a + b; } template<typename T> T foo(int a, T b) { return foo(T(a), b); } template<typename T> T foo(T a, int b) { return foo(a, T(b)); }
would not compile, giving me the messageCode:foo(0, 0)
So, I'm looking for a way to make both foo(0, 0.5) and foo(0, 0) compile. Any ideas? Thanks in advance.Code:error: call of overloaded 'foo(int, int)' is ambiguous candidates are: T foo(T, T) [with T = int] note: T foo(int, T) [with T = int] note: T foo(T, int) [with T = int]



2Likes
LinkBack URL
About LinkBacks



