c++ {*this} inside curly braces

When the curly braces are present, you’re copy-list-initializing the return value, no copy/move constructor is involved. The return value is constructed in-place using the Position(const Obj&) constructor.

Note that the code would fail to compile even with the curly braces if you made the Position(const Obj&) constructor explicit because copy-list-initialization does not allow explicit constructors to be called.

If you omit the curly braces, then semantically a temporary Position object is constructed within the function, and the return value is move constructed from that temporary. In practice, most implementations will elide the move construction, but it still requires a viable move constructor to be present, which is not the case here because it has been explicitly deleted. This is the reason your code will not compile without braces.

Using a C++17 compiler, your code will compile even without the curly braces because of guaranteed copy-elision.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)