Boost program_options

in program_options::parse_command_line - why can't the the argument "argv" be of type 'const charT*' rather than 'charT*'? i ask because our unit tests were giving a bunch of errors because they were written like this.. vector< char* > cmdLine; cmdLine.push_back( "someExec" ); cmdLine.push_back( "--optflag1" ); cmdLine.push_back( "value1" ); etc. then... char ** argv = &cmdLine[ 0 ]; then passing this to parse_command_line. when we compile under g++ 4.x we get warnings that assigning string literals to char* is a deprecated conversion. Actually I agree with the rationale, but this leaves me with a problem calling parse_command_line. Is it going to change any of these values? I know the c standard calls for the argv in main's prototype to be non-const, but why can't parse_command_line be more restrictive?

Steve Nolen wrote:
in program_options::parse_command_line - why can't the the argument "argv" be of type 'const charT*' rather than 'charT*'?
i ask because our unit tests were giving a bunch of errors because they were written like this..
vector< char* > cmdLine; cmdLine.push_back( "someExec" ); cmdLine.push_back( "--optflag1" ); cmdLine.push_back( "value1" ); etc.
then... char ** argv = &cmdLine[ 0 ];
then passing this to parse_command_line. when we compile under g++ 4.x we get warnings that assigning string literals to char* is a deprecated conversion. Actually I agree with the rationale, but this leaves me with a problem calling parse_command_line. Is it going to change any of these values?
No, it's not actually changing anything.
I know the c standard calls for the argv in main's prototype to be non-const, but why can't parse_command_line be more restrictive?
Because it that case, you won't be able to pass main's argv to parse_command_line. - Volodya

Hi Steve, Though I am not at all familiar with program_options but may be your situation has something to do with the fact that the type of a string literal in standard C++ is "array of the appropriate number of const characters" - "The C++ Programming Language, 3rd Ed." by Bjarne Stroustrup says so. It also says that you can assign a string literal to a char* but you cannot modify a string literal through such a pointer. The book says "If we want a string that we are guaranteed to be able to modify, we must copy the characters into an array. void f() { char p[] = "Zero"; p[0] = 'R'; } " Best regards. -Asif On 8/22/09, Steve Nolen <drnuke@lanl.gov> wrote:
in program_options::parse_command_line - why can't the the argument "argv" be of type 'const charT*' rather than 'charT*'?
i ask because our unit tests were giving a bunch of errors because they were written like this..
vector< char* > cmdLine; cmdLine.push_back( "someExec" ); cmdLine.push_back( "--optflag1" ); cmdLine.push_back( "value1" ); etc.
then... char ** argv = &cmdLine[ 0 ];
then passing this to parse_command_line. when we compile under g++ 4.x we get warnings that assigning string literals to char* is a deprecated conversion. Actually I agree with the rationale, but this leaves me with a problem calling parse_command_line. Is it going to change any of these values?
I know the c standard calls for the argv in main's prototype to be non-const, but why can't parse_command_line be more restrictive?
participants (3)
-
Asif Lodhi
-
Steve Nolen
-
Vladimir Prus