why won't template deduction work in this situation
Mar 10, 2014 at 12:09pm UTC
The two anonymous objects constructed at main() inside the call to print()
requires me to supply the template arguments but it can be deduced ? right ?
GCC says :
error: missing template arguments before '(' token
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
using namespace std;
template <typename F>
void print( F f )
{
cout << f() << endl;
}
template <typename T>
struct Val {
Val( T val ) : val(val) { }
T operator () () const { return val; };
T val;
};
int main()
{
print( Val(3) ); // << here
print( Val("Hello World" ) ); // << here
return 0;
}
Last edited on Mar 10, 2014 at 12:10pm UTC
Mar 10, 2014 at 12:45pm UTC
Can not deduce. 'Val' is not a typename. Val<int> is a typename.
std::pair has a std::make_pair helper though.
1 2 3 4 5 6 7 8
template <typename T>
Val<T> makeVal( T t )
{
return Val<T>(t);
}
// ...
makeVal(3) // this is ok, it calls 'makeVal' with T=int
Topic archived. No new replies allowed.