Is this a new C ++ 11 comment style?

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 } 
+12
c ++ gcc comments c ++ 11 g ++
Apr 12 '14 at 16:26
source share

No one has answered this question yet.

See similar questions:

70
Strange code that compiles with g ++

or similar:

6956
Can comments be used in JSON?
1783
C ++ 11 introduced a standardized memory model. What does it mean? And how will this affect C ++ programming?
1391
What is a lambda expression in C ++ 11?
1240
Comments in Markdown
1215
Why is Java code executing in comments with some Unicode characters?
1087
How do you block comments in YAML?
1065
How to create multi-line comments in Python?
1053
What is a quick way to comment / uncomment lines in Vim?
787
What is the difference between "typedef" and "use" in C ++ 11?
597
Multi-line comments in Ruby?



All Articles