cxxopts icon indicating copy to clipboard operation
cxxopts copied to clipboard

default_value /implicit_value of type unsigned int cause logic error

Open bwlee opened this issue 6 years ago • 6 comments

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!

bwlee avatar Feb 02 '19 16:02 bwlee

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.

frysztak avatar Mar 03 '19 08:03 frysztak

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?

TheQue42 avatar May 16 '22 16:05 TheQue42

@jarro2783 The problem with integers can be avoided with some template magic: https://github.com/artpaul/cxxopts/blob/1e58f5627fed3e925e5cb54e0f38da1be6c51e27/include/cxxopts.hpp#L387

artpaul avatar May 16 '22 21:05 artpaul

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

vintage-camera-pictures avatar May 27 '23 03:05 vintage-camera-pictures

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.

jarro2783 avatar May 29 '23 07:05 jarro2783

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"?

TheQue42 avatar Oct 02 '23 11:10 TheQue42