cxxopts
cxxopts copied to clipboard
default_value /implicit_value of type unsigned int cause logic error
code as below, and I tried both default_value and implicit value, got same error:
options.add_options() ("t,trigger", "trigger on.", co::value<unsigned int>()->default_value(0u))
which compiled but given such error:
terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid
Thanks for help!
See here https://github.com/jarro2783/cxxopts#default-and-implicit-values:
Note that the default and implicit value is always stored as a string, regardless of the type that you want to store it in. It will be parsed as though it was given on the command line.
Could this be raised as a feature-request today? To make it possible to have a default value of the actual type you intend to use?
@jarro2783 The problem with integers can be avoided with some template magic: https://github.com/artpaul/cxxopts/blob/1e58f5627fed3e925e5cb54e0f38da1be6c51e27/include/cxxopts.hpp#L387
options.add_options()("w, width", "width", cxxopts::value<int>()->default_value(0))
Compiles and crashes with a segmentation fault (Note that the parameter to default_value
is of type int
that by some compiler magic is converted to const std::string&
). The expected behaviour is to produce a compilation error.
cxxopts version 3.1.1 Platform: Win10; Visual Studio Build Tools 2022 - amd64
I guess every C++ program in the world has this problem. But since it seems to happen to people commonly here I will add this template fix.
Since this is classified as a "new issue", I'm guessing this still hasnt been implemented?
And it seems that my goal is a bit "larger" that I think the referenced solution above tries to solve, since that wont support any generic default value, such as bool? I still have to send in "False"?