Boost logo

Boost :

Subject: Re: [boost] [program_options]
From: Vladimir Prus (vladimir_at_[hidden])
Date: 2010-03-07 01:25:54

vicente.botet wrote:

> Hi,
> ----- Original Message -----
> From: "Phil Richards" <news_at_[hidden]>
> To: <boost_at_[hidden]>
> Sent: Saturday, March 06, 2010 11:59 AM
> Subject: Re: [boost] [program_options]
>> On Fri, 2010-03-05 at 09:21 +0300, Vladimir Prus wrote:
>>> Matthew Herrmann wrote:
>>> > I'd vote to replace const char* everywhere in the program options
>>> > interface with std::string. (excluding the "char* argv[]" of course)
>>> 'replace'? I'm afraid, over my dead body only. I surely don't want
>>> implicit conversion to happen, and bloat the code, for every name and
>>> description of the option.
>> Well, I certainly don't want to see anybody's dead body, but here is the
>> code for setting name:
>> option_description::set_name(const char* _name)
>> {
>> std::string name(_name);
>> string::size_type n = name.find(',');
>> if (n != string::npos) {
>> assert(n == name.size()-2);
>> m_long_name = name.substr(0, n);
>> m_short_name = '-' + name.substr(n+1,1);
>> } else {
>> m_long_name = name;
>> }
>> return *this;
>> }
>> I can't see how taking a std::string const& as an argument has any
>> detrimental effect on conversions or code bloat. The std::string
>> version of set_name would just remove the first line of the function
>> (creating the temporary "name"), and use _name throughout the function
>> body.

Sorry, you have said 'replace' -- which means changing const char* to
std::string (const, &, whatever). That would meant creating a string
at each call site where you pass const char*, which is more code than
required to just push pointer to the stack.

>> Description is even simpler: it just uses the const char* argument as a
>> parameter to a std::string constructor. Accepting a std::string const&
>> instead of a const char* will have minimal overheads (possibly one extra
>> temporary string creation).
>> But maybe I'm missing something obvious?
> The fact that currently the implementation use a temporary string doesn't means that we can not
> change the implementation. The interface with a string const reference involves already an
> allocation and deallocation when you have a const char*.
> What about having both overloads? const char * is the best for literals, while string cont& is the
> best for strings variables.
> void option_description::set_name(string const&_name);
> void option_description::set_name(const char* _name);

As I've said already, I don't remember why this is not provided, and will have to think about

- Volodya

Boost list run by bdawes at, gregod at, cpdaniel at, john at