struct Bob
{
template<class T>
void operator () () const
{
T t;
}
template<class T>
operator T () const
{
T t;
return t;
}
};
I can directly call Bob's operator() like this
Bob b;
b.operator()<int>();
How to directly call the conversion operator with a specific template parameter like this?
Bob b;
std::string s = b.???<std::string>();
It's not possible to use static_cast
Bob b;
std::string s = static_cast<std::string>(b);
http://ideone.com/FoBKp7
error: call of overloaded ‘basic_string(Bob&)’ is ambiguous
Question How to call directly with template parameter OR it's not possible. I know there are workarounds using a wrapping function.
You can call it directly (explicitly) like this:
Bob b;
std::string s = b.operator std::string();
but it's not "with a specific template parameter" (but there's no need for).
See also WhozCraig's comment
Use a helper function:
template< typename T >
T explicit_cast( T t ) { return t; }
int main()
{
Bob b;
std::string s = explicit_cast<std::string>(b);
}
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