I have the following code:
template <typename T>
void f1( T t )
{
std::cout << "f1( " << t << " ) called." << endl;
}
template <typename T>
void f2( T t )
{
std::cout << "f2( " << t << " ) called." << endl;
}
template <typename F, typename T>
void call( F && f, T t )
{
f( t );
}
template <typename T>
void foo( T t )
{
call( f1<T>, t ); // Why is <T> necessary?
// f1(t) is a valid expression!
call( f2<T>, t );
}
void bar()
{
foo( 1 );
}
In the function foo()
I need to specify the template argument, even though f1(t)
is a valid expression. That's kinda destroying some possibilities in my code. My questions:
- Why do I need to specify the template argument?
- How can I work around that limitation? (C++11 or C++14 allowed).
(BTW: I'm currently using Visual Studio 2010 and I get the error C2896, if I leave the <T>
out.)