0

NOTE: Please see edit to see why it's not a duplicate.

Note the use of ({..}) inside the print() call.

#‎include‬ <iostream>
#include <sstream>

using namespace std;

void print(const std::string& msg) {
  cout << msg << endl;
}

int main() {
  print(({
    ostringstream s;
    s << "hello";
    s << " world";
    s.str();
  }));
}

This construct is used in the Linux kernel code--which is where I first encountered it--usually in complex macro definitions.

Turns out g++ will also compile this snippet with no warnings, even using -Wall -Wextra.

Is this thing really standard C and C++ code? If so, how is it called, and where can I find it defined in the latest C++ (in particular) standard?

Additionally, if it's standard, is there anything one should keep in mind while using it in C++? For example, what are the copy/move semantics of the scope's "return value" (if you can call it that)? What are some possible drawbacks of using this construct? (Aside from some people not knowing or liking it.)

EDIT: There is this question which deals with ANSI C. Are compound statements (blocks) surrounded by parens expressions in ANSI C?

So now the question is rather, is it legit C99 or C++11?

Community
  • 1
  • 1
Yam Marcovic
  • 7,953
  • 1
  • 28
  • 38
  • 9
    see [Are compund statements (blocks) surrounded by parens expressions in ANSI C?](http://stackoverflow.com/q/1238016/1708801) – Shafik Yaghmour Aug 11 '15 at 19:05
  • Linux is written in C, how could C++ turn up? – edmz Aug 11 '15 at 19:07
  • @black Linux kernel does use it. OP said "this construct is used", not this program ;-) – P.P Aug 11 '15 at 19:08
  • @black I personally tried it in C++ context, since that's my main language. – Yam Marcovic Aug 11 '15 at 19:09
  • @chris g++ indeed compalins even with -std=c++11. Will you please confirm this is not a mistake on its part and answer? I'll accept such answer. Thanks. – Yam Marcovic Aug 11 '15 at 19:14
  • "So now the question is rather, is it legit C99 or C++11?" -- No. It's not. – P.P Aug 11 '15 at 19:15
  • @BlueMoon Do you see any easy way of verifying this? E.g. recent explicit GCC doc? Or am I being too optimistic and there's really no way this is standard? – Yam Marcovic Aug 11 '15 at 19:16
  • 1
    It's a gcc extension and it's not part of the standard C11 or C++11. If you want proof then read the standard copies (or publicly available drafts) and you won't find any reference to it OR ask your compiler to strictly compile in ISO mode. – P.P Aug 11 '15 at 19:18
  • @BlueMoon By easy I meant not going through the whole standard A to Z. Tried it once, and not going to try again in a while if I can help it. If there's no such easy way then I'll let it go at that. :) – Yam Marcovic Aug 11 '15 at 19:19
  • 1
    [Statement expressions](https://gcc.gnu.org/onlinedocs/gcc-5.2.0/gcc/Statement-Exprs.html#Statement-Exprs) are a GCC extension, listed as extension number 1 in the documentation. I've cited the version 5.2.0 documentation; you'd find it listed as an extension in earlier versions too. – Jonathan Leffler Aug 11 '15 at 19:20
  • @JonathanLeffler Great! If 5.2 still considers it an extension, that settles it for me. Thank you, sir! – Yam Marcovic Aug 11 '15 at 19:21

0 Answers0