c++ - Strange code that compiles with g++ -


the following code compiles g++ 4.8.1:

int main() {     int(*)(); } 

it looks simple declaration of pointer function:

int(*f)(); 

it doesn't compile clang 3.4 , vc++ 2013.

is compiler bug or 1 of dark places of standard?


list of similar strange code pieces compile fine g++ 4.8.1 (updated):

  1. int(*)();

  2. int(*);

  3. int(*){};

  4. int(*());

live example these strange code pieces.

update 1: @ali added interesting information in comments:

all 4 cases give compile error clang 3.5 trunk (202594) , compile fine gcc 4.9 trunk (20140302). behavior same -std=c++98 -pedantic, except int(*){}; understandable; extended initializer lists available -std=c++11.

update 2: @cantchooseusernames noted in his answer still compile fine initialization , no assembly generated them g++ (neither nor without initialization) without enabled optimization:

  1. int(*)() = 0;

  2. int(*) = 0;

  3. int(*){} = 0;

  4. int(*()) = 0;

live example initializations.

update 3: surprised find int(*)() = "hello, world!"; compiles fine, (while int(*p)() = "hello, world!"; doesn't compile, of course).

update 4: fantastic int(*){} = hello, world!; compiles fine. , following extremely strange piece of code, too: int(*){}() = -+*/%&|^~.,:!?$()[]{}; (live example).

update 5: @zwol noted in his comment

this , number of related syntactic problems being tracked gcc bug 68265.

according c++ standard (p. #6 of section 7 declarations)

6 each init-declarator in init-declarator-list contains 1 declarator-id, name declared init-declarator , hence 1 of names declared declaration

so compiler bug.

the valid code example (apart function pointer declaration showed you) though can not compile ms vc++ 2010.

int(*p){}; 

it seems compiler using testing allows declarations without declarator-id.

also take account following paragraph of section 8.1 type names

1 specify type conversions explicitly, , argument of sizeof, alignof, new, or typeid, name of type shall specified. can done type-id, syntactically declaration variable or function of type omits name of entity.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -