C++ 当使用std::map时,包含唯一\u ptr的结构中的默认析构函数会导致编译错误

C++ 当使用std::map时,包含唯一\u ptr的结构中的默认析构函数会导致编译错误,c++,dictionary,destructor,unique-ptr,C++,Dictionary,Destructor,Unique Ptr,考虑下面的代码示例,为什么默认析构函数的定义会导致编译错误 #include <iostream> #include <memory> #include <map> struct Foo { char c; std::unique_ptr<int> ptr; Foo(char c_, int n_) : c(c_), ptr(std::make_unique<int>(n_)) {;} //

考虑下面的代码示例,为什么默认析构函数的定义会导致编译错误

#include <iostream>
#include <memory>
#include <map>

struct Foo
{
    char c;
    std::unique_ptr<int> ptr;

    Foo(char c_, int n_) : c(c_), ptr(std::make_unique<int>(n_))
    {;}

    //~Foo() noexcept = default; // problem here, why?
};


int main()
{
    std::map<int, Foo> mp;

    mp.emplace(0, Foo{'a',40});
    mp.emplace(1, Foo{'b',23});

    for (auto &&i : mp)
        std::cout<< i.first << " : {" << i.second.c << "," << *(i.second.ptr) << "}" <<std::endl;
}
#包括
#包括
#包括
结构Foo
{
字符c;
std::唯一的ptr ptr;
Foo(char c_u,int n_u):c(c_u),ptr(std::make_uunique(n_u))
{;}
//~Foo()noexcept=default;//这里有问题,为什么?
};
int main()
{
std::map-mp;
下院议员席位(0,Foo{'a',40});
议员职位(1,Foo{'b',23});
用于(自动和输入:mp)

std::cout因为如果您自己定义析构函数,编译器将不再为您生成复制con和移动con。您可以将它们指定为默认值并将其删除(即使由于
唯一性\u ptr
,复制con仍将被隐式删除),并且您的代码将再次工作:

struct Foo
{
    char c;
    std::unique_ptr<int> ptr;

    Foo(char c_, int n_) : c(c_), ptr(std::make_unique<int>(n_))
    {;}

    ~Foo() noexcept = default;
    Foo(Foo&&) = default;
};
structfoo
{
字符c;
std::唯一的ptr ptr;
Foo(char c_u,int n_u):c(c_u),ptr(std::make_uunique(n_u))
{;}
~Foo()noexcept=默认值;
Foo(Foo&&)=默认值;
};

<>这也是“代码>运算符=(Fo&&)。当然,

当然!!AAAARG!!现在我记得用有效的现代C++读取它。实际上定义析构函数阻止创建一个移动构造函数。复制构造函数是从C++ 98中生成的!谢谢!