I was creating a function in Visual Studio 2012 to check if an item was the default and went to call it default(). However, I noticed that when I finished and waited a couple of seconds, the name was no longer bolded and no Intellisense errors were shown. Intrigued, I tried it out:
void default(){}
int main() {
default();
}
This code works on Visual Studio 2012 with both the regular compiler and the November 2012 CTP one, but does not compile on GCC 4.7.2 or on http://llvm.org/demo. I realize that default is used for both switches and for = default on some class members. VS2012 does not support the latter, but it obviously supports the former, and it does indeed complain if I change the function name to delete, nullptr, or switch, or case.
On GCC, I get this:
error: expected unqualified-id before ‘default’
In function ‘int main()’:
error: case label not within a switch statement
error: expected ‘:’ before ‘(‘ token
error: expected primary-expression before ‘)’ token
Clang gives me this:
error: expected unqualified-id
void default(){}
……..^
I’m obviously changing the name of it now, but is VS wrong in accepting this, or did it perhaps change specifically with the = default and = delete addition to the language? If VS doesn’t support that, and that’s the cause of the program being wrong, I could see why it accepts it.
I found this in the spec (C++11 § 6.1/2):
Case labels and default labels shall occur only in switch statements.
Since it is not being used as a label, with the colon following it, I guess it could be argued that it’s allowed, but then why would case compile? I’m guessing there’s something more specific to function names, but I couldn’t find it.
Are you allowed to name a function default(), and does the rule differ between C++03, and C++11?
The
defaultidentifier is reserved for use as a keyword:Table 4† contains, among all other keywords,
default. VC++ is wrong to accept an identifier ofdefaultoutside of an attribute-token, and a function name is not an attribute-token..† It’s Table 4 in C++11, but Table 3 in C++98 and 03. Nonetheless, they all contain
default.