Modnar
Modnar

Reputation: 21

Why Xcode's default code generated is wrong?

When I create a C++ project using Xcode, I will get the code generated by Xcode like this:

int main(int argc, const char * argv[]) {
...
}

However, the code even could not pass the compile when I use getopt(argc, argv, nullptr), which is defined in <unistd.h>.

The reason of the problem is parameter is invalid, so why Xcode does not generate the code like char * const argv[], which could at least pass the compiling stage.

Xcode's build failed information

Upvotes: 1

Views: 90

Answers (1)

Asteroids With Wings
Asteroids With Wings

Reputation: 17454

You're right; this was a bad decision on Apple's part.

Though directly modifying argv's elements is usually frowned upon, it's not inherently "wrong" and indeed some functions like getopt expect to be able to. Adding constness there in the default code template not only breaks this, but also goes against the standard's guidelines:

An implementation shall allow both

  • a function of () returning int and
  • a function of ( int, pointer to pointer to char ) returning int
    as the type of main ([dcl.fct]).

(Though note that this passage isn't claiming that those are the only valid types for main.)

Personally I'd prefer Xcode did not do this. Perhaps suggesting an improvement via Radar would be helpful.

Upvotes: 1

Related Questions