Consider:
struct Foo
{
Foo(std::string str) {}
};
struct Bar
{
Bar(Foo f) {}
};
int main(int argc, char* argv[])
{
Foo f("test");
Bar b1(f);
Bar b2(std::string("test"));
Bar b3("test");
return 0;
}
This fails to compile on the declaration of b3 ('cannot convert argument 1 from 'const char [5]' to 'Foo''). Which makes sense, because there is no direct way to convert the const char to a Foo. However, there is a way to convert the const char to a std::string, and then use that to construct a Foo (which is what is happening in b1 and b2), and that is what I want because it makes the API nicer to use (not having to instantiate a Foo or an std::string explicitly every time).
So my question is: is there a way to let the compiler implicitly call the Foo(std::string) copy constructor? In other words, is there a way to make a declaration like that of b3 work, let it be the same as b2, and without declaring a const char* copy constructor for Foo? (that last thing is the obvious way but my real code is of course not as simple as this, and I'd prefer not having to add const char* copy constructors and handling all of the other initialisation in the constructors correctly and keeping that in sync with the std::string copy constructor).
const char*tostd::string, thenstd::stringtoFoo). One way to go would be to add aFooconstructor overload takingconst char*. - Igor Tandetnik