I have code like the following:
class Foo
{
public:
template<typename R, typename T, typename S>
static R add(T x, S y) { return x + y; }
};
template<typename Foo, typename T>
inline T add(T x, double y)
{
return Foo::add<T, T, double>(x, y);
}
My expectation was to obtain a free function callable like SomeType add<Foo>(SomeType, double)
.
(The idea is that Foo
and other classes that define a similar templated add
need only specify the calculation once, and the return type disambiguation is done by a number of free functions.)
Instead, the code fails to compile (https://ideone.com/ZI0v0M):
prog.cpp: In function ‘T add(T, double)’:
prog.cpp:11:19: error: expected primary-expression before ‘,’ token
return Foo::add<T, T, double>(x, y);
^
prog.cpp:11:22: error: expected primary-expression before ‘,’ token
return Foo::add<T, T, double>(x, y);
^
prog.cpp:11:24: error: expected primary-expression before ‘double’
return Foo::add<T, T, double>(x, y);
^~~~~~
prog.cpp:11:23: error: expected ‘;’ before ‘double’
return Foo::add<T, T, double>(x, y);
^~~~~~~
;
prog.cpp:11:30: error: expected unqualified-id before ‘>’ token
return Foo::add<T, T, double>(x, y);
^
Why is T
not being interpreted as the template parameter here?