Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

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

The following code compiles successfully with g++ 4.8.1:

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

It looks like a simple declaration of a pointer to function:

int(*f)();

It doesn't compile with clang 3.4 and vc++ 2013.

Is it a compiler bug or one of dark places of the standard?


List of similar strange code pieces which compile fine with g++ 4.8.1 (updated):

  1. int(*)();

  2. int(*);

  3. int(*){};

  4. int(*());

Live example with these strange code pieces.

Update 1: @Ali added some interesting information in the comments:

All 4 cases give a compile error with clang 3.5 trunk (202594) and compile fine with gcc 4.9 trunk (20140302). The behavior is the same with -std=c++98 -pedantic, except for int(*){}; which is understandable; extended initializer lists only available with -std=c++11.

Update 2: As @CantChooseUsernames noted in his answer they still compile fine even with initialization and no assembly is generated for them by g++ (neither with nor without initialization) even without any enabled optimization:

  1. int(*)() = 0;

  2. int(*) = 0;

  3. int(*){} = 0;

  4. int(*()) = 0;

Live example with initializations.

Update 3: I was really surprised to find that int(*)() = "Hello, world!"; compiles fine, too (while int(*p)() = "Hello, world!"; doesn't compile, of course).

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

Update 5: As @zwol noted in his comment

This and a number of related syntactic problems are being tracked as gcc bug 68265.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the C++ Standard (p. #6 of section 7 Declarations)

6 Each init-declarator in the init-declarator-list contains exactly one declarator-id, which is the name declared by that init-declarator and hence one of the names declared by the declaration

So it is simply a compiler bug.

The valid code could look as for example (apart from the function pointer declaration showed by you) though I can not compile it with my MS VC++ 2010.

int(*p){};

It seems that the compiler you are using for testing allows declarations without a declarator-id.

Also take into account the following paragraph of section 8.1 Type names

1 To specify type conversions explicitly, and as an argument of sizeof, alignof, new, or typeid, the name of a type shall be specified. This can be done with a type-id, which is syntactically a declaration for a variable or function of that type that omits the name of the entity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...