Modify

Opened 8 years ago

Closed 7 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[],
                   ....)
                  

Attachments (0)

Change History (7)

comment:1 Changed 8 years ago by anonymous

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 Changed 8 years ago by vladimir

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

comment:3 Changed 8 years ago by anonymous

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 Changed 8 years ago by ilyasokol@…

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 Changed 7 years ago by Marshall Clow

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

comment:6 Changed 7 years ago by Marshall Clow

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

comment:7 Changed 7 years ago by Vladimir Prus

Resolution: fixed
Status: newclosed

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

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Vladimir Prus.
The resolution will be deleted.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.