I'am trying to do this work:
template < typename T, T VALUE >
void f()
{
/* ... */
}
int main()
{
f<10>(); // implicit deduction of [ T = int ] ??
return (0);
}
The purpose is to simplify a much more complex template.
After many searches, I don't find any way to do that on C++0x, so stackoverflow is my last resort.
C++0x introduces decltype()
, which does exactly what you want.
int main()
{
f<decltype(10), 10>(); // will become f<int, 10>();
return 0;
}
There is no automatic template deduction for structs/classes in C++. What you can do though is something like this (warning, untested!):
#define F(value) f<decltype(value), value>
template < typename T, T VALUE >
void f()
{
/* ... */
}
int main()
{
F(10)();
return (0);
}
It is not as clean as template only code but it's clear what it does and allows you to avoid the burden of repeating yourself. If you need it to work on non-C++0x compilers, you can use Boost.Typeof instead of decltype.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With