|
Boost : |
Subject: Re: [boost] [program_options]
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2010-03-06 07:22:05
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.
>
> 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);
If the current implementation uses a temporary string, it needs just to forward the call.
void option_description::set_name(const char* _name) {
std::string name(_name);
option_description::set_name(name);
}
Just my 2cts,
Vicente
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk