c ++ initializer_list i shared_ptr zachowanie [duplikat]

To pytanie ma już tutaj odpowiedź:

Podwójne usuwanie w initializer_list vs 2013 1 odpowiedź

Testuję listę inicjalizującą vs2013 c ++.

Poniższy kod można skompilować. Ale ulega awarii, gdy uruchamiam exe.

#include <memory>
#include <iostream>

class Base {};

class Derived : public Base {};

void DoSomething(std::initializer_list<std::shared_ptr<Base> > list)
{
}

int main()
{
  auto ip = std::make_shared<Derived>();

  std::cout << "use_count=" << ip.use_count() << std::endl;

  DoSomething({ip, std::make_shared<Derived>()}); // ng
  // DoSomething({ip, std::make_shared<Base>()}); // ok
  // DoSomething({std::make_shared<Derived>(), ip}); // ok

  std::cout << "use_count=" << ip.use_count() << std::endl;
}

Kompiluje.

C:\...>cl.exe /EHsc test.cpp
Microsoft(R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
Microsoft (R) Incremental Linker Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

C:\...>

Spodziewałem się takiego wyniku. g ++ 4.8.2 działa tak.

c:\...>test.exe
use_count=1
use_count=1

Jednak wygląda tak.

c:\...>test.exe
use_count=1
use_count=0 // or some random value like 3719232 and displayed crash dialog. 

I Modyfikując jeden wiersz powyższego kodu, to działa dobrze.

DoSomething({std::make_shared<Derived>(), ip});

Czy jest to błąd lub normalne zachowanie listy inicjalizującej vs2013?

questionAnswers(1)

yourAnswerToTheQuestion