Opened 13 years ago

Closed 12 years ago

#3909 closed Bugs (fixed)

parse_command_line has bad declaration

Reported by: anonymous Owned by: Vladimir Prus
Milestone: Boost 1.43.0 Component: program_options
Version: Boost 1.42.0 Severity: Problem
Keywords: Cc:

Description

The parse_command_line must receive const char* const argv[].

Change:

template<class charT>
basic_parsed_options<charT>
parse_command_line(int argc, charT* 'const' argv[],
                   ....)
                  

Change History (7)

comment:1 by anonymous, 13 years ago

Also there is a problem with constness in charT.

This causes problem:

int main(int argc, char const* const argv[])
{
  parse_command_line(argc, argv);
}
  1. parse_command_line cannot receive char const* const
  2. charT = char const , there is no specialization for basic_parsed_options<char const> So there is a need to remove_cv<charT> and only then specialize basic_parsed_options.


template<class charT>
basic_parsed_options<typename remove_cv<charT>::type>
parse_command_line(int argc, charT* const argv[],
                   ....)

comment:2 by vladimir, 13 years ago

Could you clarify? The type of argv you use in your example is in violation of the C++ standard, I believe.

comment:3 by anonymous, 13 years ago

I am not sure about violation because adding const does not destroy anything. But the Standard says about int main(int argc, char* argv[]), so from this point of view you are right.

Anyway I don't know if there is a compiler which rejects main function with char const* const argv[]. Event Comeau compiler accepts it.

I suggest you to add remove_cv anyway, it will make code more correct. Thank you.

comment:4 by ilyasokol@…, 12 years ago

See these use cases:

http://lists.boost.org/boost-users/2006/01/16620.php http://lists.boost.org/boost-users/2009/08/50968.php

The signature can be changed as follows:

template<class charT>
basic_parsed_options<typename remove_cv<charT>::type>
parse_command_line(int argc, const charT* const argv[],
                   ....)

or

template<class charT>
basic_parsed_options<typename remove_cv<charT>::type>
parse_command_line(int argc, const charT* const* argv,
                   ....)

(The second is a bit more readable for me)

There is no need to remove_cv<charT> and the change is fully backward compatible.

comment:5 by Marshall Clow, 12 years ago

(In [66959]) Take argv as const; refs #3909

comment:6 by Marshall Clow, 12 years ago

(In [67006]) patch tests for Sun; refs #3909

comment:7 by Vladimir Prus, 12 years ago

Resolution: fixed
Status: newclosed

I assume this is fixed by earlier commits. Thanks Marshall!

Note: See TracTickets for help on using tickets.