cxxopts: Compiler error when calling parse function with mutable argv

Since #99 was merged (I’m using cxxopts 2.1.0 now), simple usage examples like the following fail:

#include <cxxopts.hpp>

int main(int argc, char *argv[])
{
	cxxopts::Options options("test");
	options.parse(argc, argv);

	return 0;
}

with the error message (g++ 7.3.1):

test.cpp: In function ‘int main(int, char**)’:
test.cpp:6:26: error: invalid conversion from ‘char**’ to ‘const char**’ [-fpermissive]
  options.parse(argc, argv);
                          ^
In file included from test.cpp:1:0:
cxxopts.hpp:1642:1: note:   initializing argument 2 of ‘cxxopts::ParseResult cxxopts::Options::parse(int&, const char**&)’
 Options::parse(int& argc, const char**& argv)
 ^~~~~~~
test.cpp:6:26: error: cannot bind rvalue ‘(const char**)argv’ to ‘const char**&’
  options.parse(argc, argv);

The reason is that char *argv[] (or char **argv) cannot be implicitly converted to const char **, which is used in all parse function signatures.

I know that #99 added the const qualifier for a reason. I like the idea suggested by @vladimirgamalyan in #91 to turn const char **&argv in the parse function signatures into const char * const *&argv. However, I think that’s not possible right now, because argv is modified by cxxopts such as in:https://github.com/jarro2783/cxxopts/blob/0fe1dc892bcbef48b3911510209c7033f31ce05e/include/cxxopts.hpp#L1681

Still, I think that this should be addressed, because int main(int argc, char *argv[]) is a standard main function signature, which should be supported by cxxopts. If that’s not feasible, it would be good to mention this in the readme at least 🙂.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 17 (4 by maintainers)

Most upvoted comments

I’ve reverted the offending commit in cc4914f.

@jarro2783 Can you please make a release with this change?

Sure. I’ll do another release soon.

I’ve reverted the offending commit in cc4914f0.

Sorry about that. I didn’t look closely enough at that PR and it looked safe with the tests passing. I’ll come up with a fix.