In C++, what do braces on the left-hand side of a variable declaration mean?

  • A+
Category:Languages

The code at https://github.com/HertzDevil/0CC-FamiTracker/blob/e643e8cbad1c8bf01ff706245d72f93c33417de8/Source/FamiTracker.cpp#L446 uses a C++ variable "declaration" syntax I'm not familiar with:

std::unique_ptr<CRecentFileList> {m_pRecentFileList} = std::make_unique<CRecentFileList>(... 

(m_pRecentFileList is declarared in a superclass.)

What does it mean when you wrap a variable declaration in braces? (not initializer list)


I extracted a minimal test case which compiles:

class foo {     int* p;     void f(){         std::unique_ptr<int> {p} = std::make_unique<int>(1);     } }; 

Changing int* p to std::unique_ptr<int> p creates a compilation error due to unique_ptr(const unique_ptr&) = delete;

This makes me think braced declaration assigns to a outer-scope variable with the same name. I tried creating a test program, but it fails to compile:

int main(){     int x;     int {x} = 1; } 

error: using temporary as lvalue [-fpermissive]

 


It's not a declaration. It's an assignment to a temporary.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: