Circumvent c++ null-terminated string frustration
- by ypnos
I'm using boost::program_options and it suffers from the same as many other c++ libs, even std itself: It still uses C-style null-terminated strings, because nobody really likes the weak std::string.
The method in question is:
options_description_easy_init&
operator()(const char* name,
const value_semantic* s,
const char* description);
The typical use case is just fine:
options.add_options()
("graphical", bool_switch(&isGraphical)->default_value(false),
"Show any graphical output during runtime")
However, I need the name of the option to be set dynamically. The reason is that in some cases I nead a custom prefix, which is added to the string by my function std::string key(const std::string& k):
options.add_options()
(key("graphical"), bool_switch(&isGraphical)->default_value(false),
"Show any graphical output during runtime")
This fails.
I could now use c_str() on the std::string but that's evil -- I don't know how long program_options keeps the variable around and if my string is still alive when needed.
I could also reserve memory in a buffer etc. and hand in that. The buffer is never freed and it sucks/is evil.
Is there anything else I can do to circumvent the C-style string mess in this situation?