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 will not compile when declaring b3 ('cannot convert argument 1 from' const char [5] 'to' Foo ''). This makes sense because there is no direct way to convert const char to Foo. However, there is a way to convert const char to std :: string, and then use it to build Foo (this is what happens in b1 and b2), and this is what I want because it makes the API better not to use (it is not necessary to create an instance of Foo or std :: string each time).
So my question is: is there a way to allow the compiler to implicitly call the copy constructor Foo (std :: string)? In other words, is there a way to make an declaration similar to the description of b3, let it be the same as b2, and without declaring the copy char constructor const char * for Foo? (that the latter is the obvious way, but my real code, of course, is not as simple as this, and I would prefer not to add const char * copy constructors and correctly handle all other initialization in the constructors and keep that in sync with the std copy constructor: : string).
source share