Opened 13 years ago

Last modified 11 years ago

#4142 new Feature Requests

Option names should be constructible from strings

Reported by: afoglia@… Owned by: Vladimir Prus
Milestone: Boost 1.43.0 Component: program_options
Version: Boost 1.42.0 Severity: Cosmetic
Keywords: Cc:

Description

The constructor and set_name methods of option_description take only const char pointers and not strings. (Likewise for the option_description_easy_init operator().)

Currently code such as the below fails to compile

std::string help="help";
po::options_description desc("Allowed options");
desc.add_options()
    (HELP, "produce help message");

We use strings for easy reuse them when checking the variable map of parsed options.

This came up on the dev list last month, and Vladimir said he'd look into why overloads for strings didn't already exist.(http://lists.boost.org/Archives/boost/2010/03/162646.php) I assume you haven't had time, so here's a more persistent reminder.

Change History (4)

comment:1 by anonymous, 12 years ago

Here is one problem. Lets look at the following declaration:

options_description_easy_init::operator()(const char* name,
                                          const value_semantic* s,
                                          const char* description);

Note that you can't replace it with

options_description_easy_init::operator()(const std::string& name,
                                          const value_semantic* s,
                                          const std::string& description);

because the latter is not exception-safe due to unspecified order of arguments' evaluation.

comment:2 by Vladimir Prus, 12 years ago

I am not sure I follow what exception safety issues are there? It's only possible to leak 's' when ctor of std::string throws, which can happen only in 'out of memory' condition, and I'm not sure I care much about that use case -- especially for a library that parses program's command line.

in reply to:  2 comment:3 by anonymous, 12 years ago

Replying to vladimir_prus:

I am not sure I follow what exception safety issues are there? It's only possible to leak 's' when ctor of std::string throws,

Yes, that's what I meant.

which can happen only in 'out of memory' condition, and I'm not sure I care much about that use case -- especially for a library that parses program's command line.

It is hard to argue against such reasoning. Don't forget that B.PO parses not only command line now.

comment:4 by Olaf van der Spek <olafvdspek@…>, 11 years ago

Instead of std::string, you could use a template function and use data() and size() member functions of the passed in argument. Or, better yet, support a str_ref class (not yet available in Boost).

Note: See TracTickets for help on using tickets.