Using the brace-init command to initialize a link to std :: shared_ptr

I recently worked on code, and I came across something unusual in GCC and Clang. Using brace-init triggers a compilation error in gcc, while direct initialization works like &b = a . The code below is a very simple example of the behavior I encountered, and I was wondering why GCC does not compile the code, since none of shared_ptr accepts initializer_list, and a is an lvalue

 #include <iostream> #include <memory> int main( ) { std::shared_ptr<int> a { nullptr }, &b { a }; a = std::make_shared<int> ( 1e3 ); std::cout << ( b ? *b : 0 ) << std::endl; return 0; } 

Clang 3.4 compiles this, but GCC 4.8 does not.

+5
source share
1 answer

CWG Defect 1288 , as indicated by @Dyp, has been confirmed and fixed for GCC 4.9.0 . A workaround is to use direct initialization without initializing the list :

 // Note the parentheses std::shared_ptr<int> a { nullptr }, &b ( a ); 
+3
source

Source: https://habr.com/ru/post/1204006/


All Articles