While I was trying to understand why the code is like int(*)(); compiles fine with g ++, I found an even stranger thing:
int main() { int(*){} Is it C++11 or any other language? }
This code is compiled using g ++ 4.8.1, see a live example (flags: -std=c++11 -pedantic-errors ). Neither clang 3.4 nor vC ++ 2013 compiles it.
Is this the new C ++ 11 comment style that is only supported by g ++? Or a compiler error?
Here's what I learned about this style of "comments":
The usual structure of a comment (parts that can be omitted are enclosed in brackets [ ... ] ):
int // or another valid C++ type (*) // or another sequence of '*' and/or '&' characters with nonzero length {"[Comment header]"} [Comment body] {[Comment footnote]}
Instead of using a line terminator ; can use: int(*){} Comment ; .
- If the comment is the last construct in the
{ ... } block, both ending characters and footnotes can be omitted: { int(*){} Comment } . - Valid characters are numbers, English letters, and characters from the following list:
- + * / % & | ^ ~ = . , : ! ? $ ( ) [ ] < > - + * / % & | ^ ~ = . , : ! ? $ ( ) [ ] < > - + * / % & | ^ ~ = . , : ! ? $ ( ) [ ] < > . - The following characters are not allowed inside the body of a comment or footnote:;
; { } # @ . - The character literals
'...' and the string literals "..." allowed inside the body of a comment or footnote, but the characters ' and " cannot be used separately. Regular C ++ comments // ... and /* ... */ work fine with the new "comments". Preprocessor directives, macros, digraphs, and trigraphs also work fine ( live example ):
#define TerminatingSymbol ; int(*) ??< %> // a pair of trigraph and digraph "This is a string" // string literal #if 1 Comment #endif TerminatingSymbol std::cout << "Hello, "; int(*){} /* Character literal: */ 'c' <% ??> std::cout << "world!" << std::endl;
I could not understand how floating point literals work inside such "comments".
The following code example illustrates the possibilities of the new "comments":
#include <iostream> int main() { void(*){} do not try this float; // simple C++ keywords are allowed, of course std::cout << "1"; int(&){"Allowed characters"} - + * / % & | ^ ~ = . , : ! ? $ ( ) [ ] < > ; // ';' itself is not allowed std::cout << "2"; char(************************************) {"William Blake wrote:"} Tyger! Tyger! burning bright In the forests of the night, What immortal hand or eye Could frame thy fearful symmetry? {"The Tyger", 1794} std::cout << "3"; float(*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*) {"Some formulas"} 2 x 2 = 4 pi ~= 3.145926 ... E = m * c ^ 2 a ^ n + b ^ n != c ^ n, n > 2 {42} std::cout << "4"; unsigned(*(*(*(*(*(*(*(*(*(*())))))))))){} It is a last comment }
c ++ gcc comments c ++ 11 g ++
Constructor Apr 12 '14 at 16:26 2014-04-12 16:26
source share