7

I was working on a code recently and I stumbled on something unusual in GCC and Clang. Using brace-init triggers a compilation error in gcc, while a direct initialization such as &b = a works. The code below is a very simple example of that behaviour I encountered and I was wondering why GCC doesn't compile the code, since none of shared_ptr takes an initializer_list and a is a 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 doesn't.

iamOgunyinka
  • 1,040
  • 1
  • 9
  • 17

1 Answers1

3

The CWG Defect 1288 as pointed out by @Dyp has been acknowledged and fixed for GCC 4.9.0. A workaround is to use direct initialization without list initialization:

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