Rezaeimh7
Rezaeimh7

Reputation: 1545

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

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.

Upvotes: 6

Views: 689

Answers (1)

Destructor
Destructor

Reputation: 14438

({}); 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++.

Upvotes: 8

Related Questions