boost::optional<std::string> and implicit constructor from char[]

Viewed 5849

I have got used to initializing std::strings in the following way by letting the compiler figure out the magic involved

std::string my_string = "hello";

The following will not work as there is no explicit conversion between the two types:

boost::optional<std::string> my_optional_string = "hello";

This however does work:

boost::optional<std::string> my_optional_string = std::string("hello");

Now, would there be no way to daisychain the implicitly called single-arg constructors to allow the second form? The reason I ask (I don't want to bother you with details though) is that there are a whole bunch of classes with optional members to be populated. Having to explicitly type everything seems a burden (I do not so much worry about myself, but I am working on an open source API and would like to provide as much comfort to my users as possible). Any suggestions are appreciated.


Edit: Sorry guys I am new to this, should have provided more clarifying code examples. I have some classes (not modelled myself, just implemented them in C++) with optional members to be populated, like this:

Class Class1 {
public:
    Class1(const boost::optional<std::string>& arg_1, /*... ,*/ const boost::optional<std::string>& arg_n );
};

What I'd like my API users to be able to specify is:

Class1 my_class("hello","there",NULL,"stackoverflow");
/* ( Note: NULL is actually risky here, as some classes might also have members of type boost::optional<int> ) */

And not:

Class1 my_class(std::string("hello"),/*or*/boost::optional<std::string>("there"),boost::optional<std::string>(),std::string("stackoverflow"));

Thanks again.

2 Answers
Related