1

I'm trying to provide std::string argument as a description of option. In general, an argument whose scope of life is not defined. In details, I want to provide translation.

using namespace boost::program_options;
using namespace boost::locale;

options_description desc (translate ("Hello world!"));
desc.add_options ()
    ("help", translate ("Veni vidi vici"))
;

add_options() returns an instance of options_description_easy_init which pre-defines the () operator so that you can use the syntax above. Now, that operator accepts only const char* as description and this bugs me out. Hence I have the following questions in mind:

  1. What could be the way to approach this problem?
  2. What is the ownership policy of this class? Are these pointers only stored, hence the source, should live as much as the description lives or it is safe to (yuck!):

    ("help", translate ("Veni vidi vici").str ().c_str ())
    
  3. What is the rationale of having only const char_type* as description parameter?

1
  • 1 question at a time please
    – sehe
    Dec 12, 2016 at 13:07

1 Answer 1

0
  1. you name it, personally I'd suggest Boost Locale http://www.boost.org/doc/libs/1_61_0/libs/locale/doc/html/messages_formatting.html but maybe you already do

  2. if nothing is documented, you can assume the string is copied. Anything else would be a (documentation) bug

    It's not too hard to inspect the code for option_description and see this is indeed the case

    std::string m_short_name, m_long_name, m_description;
    // shared_ptr is needed to simplify memory management in
    // copy ctor and destructor.
    shared_ptr<const value_semantic> m_value_semantic;
    
  3. That makes it obvious for compilers to allow any C-style string/literal, while documenting to the users that embedded NUL-characters are NOT possible (implying you cannot use UCS2 or similar). Other than that, I agree that it would be more clean to declare the formal parameter as std::string.

    You might provide a pull request with the reasoning to the library maintainer(s)

2
  • I really do not like the approach with translate("abc").str().c_str(). Due to design change I will have the luck of using instead msg.c_str(). As for the rationale - const char* is stopping you of just one mistake of many (e.g. using \b and etc.). I fail to see the reasoning. That's said, I appreciate your answer - thank you.
    – Rado
    Dec 13, 2016 at 7:51
  • Like I said, you have a point and you may take it up with the devs. I'm sure they welcome input. Anyways your question was also about the safety. It's safe, I answered that.
    – sehe
    Dec 13, 2016 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.