Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Does ({}); Mean in C++?

Tags:

c++

scope

c++11

AFAIK {} defines a new scope, so what does this define?

({});

The compiler compiles this program well:

#include <iostream>
#include <string>

int main()
{
  std::string name;
  std::cout << "What is your name? ";
  {
     ({}); 
  }
  getline (std::cin, name);
  std::cout << "Hello, " << name << "!\n";
}

When i replace ({}); with (); the complier fails to compile the program.

Why does ({}); work well, but (); does not?

I have tested the program on cpp.sh. It compiles fine.

like image 404
m.r226 Avatar asked Apr 16 '17 08:04

m.r226


People also ask

What does %= mean in C?

%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C % A.

What does '#' mean in C?

'#' is called pre-processor directive and the word after '#' is called pre-processor command. Pre-processor is a program which performs before compilation.

What does %d do in C?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.


1 Answers

({}); is not part of standard C++. As correctly said by @HolyBlackCat this is compiler extension. Use -pedantic-errors to disable compiler extensions.

See live demo here when compiled on g++

See live demo here when compiled on vc++.

like image 86
Destructor Avatar answered Nov 11 '22 12:11

Destructor