Boost logo

Boost :

Subject: Re: [boost] [program_options]
From: Phil Richards (news_at_[hidden])
Date: 2010-03-06 05:59:04


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?

Phil

-- 
Phil Richards, <news_at_[hidden]>

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk