I tried implicit conversion, but this doesn’t work.
#include <string>
#include <iostream>
struct MyClass
{
operator std::string() { return "bar"; }
};
int
main( int argc, char* argv[] )
{
MyClass x;
std::cout << std::string( "foo" ) + x << std::endl;
return 0;
}
Implicit conversion won’t work since string’s
operator+is templated and you’re deducing the template parameters. This looks like a better explanation of what’s happening: https://stackoverflow.com/a/8892794/964135I would just do a cast or write a non-template
operator+.A stupid solution is to not deduce the types and then it will do the implicit conversion: