This question already has an answer here:
- Double delete in initializer_list vs 2013 1 answer
I'm testing vs2013 c++ initializer_list.
The code below can be compiled. But crashes when i run 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;
}
Compiles.
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:\...>
I expected the output like this. g++4.8.2 works like this.
c:\...>test.exe
use_count=1
use_count=1
However, looks like this.
c:\...>test.exe
use_count=1
use_count=0 // or some random value like 3719232 and displayed crash dialog.
And, Modifying one line of the code above, This works good.
DoSomething({std::make_shared<Derived>(), ip});
Is this a bug or normal behavior of vs2013 initializer_list ?