Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch::Session::applyCommandLine signature incorrect for non-const argv #615

Closed
RossBencina opened this issue Mar 19, 2016 · 2 comments
Closed

Comments

@RossBencina
Copy link
Contributor

The C and C++ standards specify that the signature for main requires argv to be modifiable.[1][2] For example: int main(int argc, char* argv[]). In particular, declaring argv const is non-portable.

If main is declared as above, then calling Catch::Session::applyCommandLine produces an error in MSVC, gcc and clang.

MSVC 2015:

main.cpp(133): error C2664: 'int Catch::Session::applyCommandLine(int,const char *[],Catch::Session::OnUnusedOptions::DoWhat)': cannot convert argument 2 from 'char *[]' to 'const char *[]'
1>         c:\amtrunk_wd\fluent\fluenttest_testmain.cpp(133): note: Conversion loses qualifiers

gcc:

prog.cc:15:29: error: invalid conversion from 'char**' to 'const char**' [-fpermissive]

clang:

prog.cc:1:6: note: candidate function not viable: no known conversion from 'char **' to 'const char **' for 2nd argument

Here's a sandbox to experiment with:
http://melpon.org/wandbox/permlink/bFwLOVsdbBMkOmUh

Proposed Fix

Change the signature of applyCommandLine and parseInto as follows:

-        int applyCommandLine( int argc, char const* argv[], OnUnusedOptions::DoWhat unusedOptionBehaviour = OnUnusedOptions::Fail ) {
+        int applyCommandLine( int argc, const char* const* argv, OnUnusedOptions::DoWhat unusedOptionBehaviour = OnUnusedOptions::Fail ) {

-        std::vector<Parser::Token> parseInto( int argc, char const* argv[], ConfigT& config ) const {
+        std::vector<Parser::Token> parseInto( int argc, const char* const* argv, ConfigT& config ) const {

This change fixes the issue in MSVC, and should do so in gcc and clang also, based on the sandbox test above.

[1] http://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function
[2] http://stackoverflow.com/questions/11216155/is-int-mainint-char-const-const-well-formed

@RossBencina
Copy link
Contributor Author

Here's the proposed fix:
RossBencina@18f18ae

@horenmar
Copy link
Member

This has been fixed in 1c47fe0, so I am going to close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants