#include <iostream>
#include <vector>
#include <memory>
using namespace std;
struct BinaryTree
{
int element;
shared_ptr<BinaryTree> left;
shared_ptr<BinaryTree> right;
};
int main()
{
vector<shared_ptr<BinaryTree>> vecBT;
// case I
vecBT.emplace_back(new BinaryTree{10, nullptr, nullptr});
// case II
vecBT.emplace_back(shared_ptr<BinaryTree>(new BinaryTree{20, nullptr, nullptr}));
return 0;
}
http://en.cppreference.com/w/cpp/container/vector/emplace_back
template< class... Args >
void emplace_back( Args&&... args );
http://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr
template< class Y >
explicit shared_ptr( Y* ptr );
问题> 我通过http://www.compileonline.com/compile_cpp11_online.php编译了上面的代码,没有错误。
我的问题是如何在case I
不产生错误的情况下通过编译器。由于构造函数shared_ptr
需要显式构造。所以我只期望case II
是正确的。