【问题标题】:Can a POD type have a default move assignment operator with explicitly declared?POD 类型可以有一个显式声明的默认移动赋值运算符吗?
【发布时间】:2023-04-10 14:30:01
【问题描述】:

考虑以下代码:

// in main.cpp
#include <type_traits>

struct A {
    A& operator=(const A&) = default;
    A& operator=(A&&) = default;
};

int main() {
    static_assert(std::is_pod<A>::value);
    return 0;
}

您可以看到struct A 有一个默认的移动赋值运算符。

msvc(VS2017) 中,使用cl /std:c++17 main.cpp,我遇到了静态断言失败。

g++(MinGW-W64, 8.1.0) 中,g++ -std=c++17 main.cpp 没有出错。

但是,如果我注释掉 A&amp; operator=(A&amp;&amp;) = default;,那么两个编译器中的一切都正常。

那么,POD 类型是否可以具有显式声明的默认移动赋值运算符?

【问题讨论】:

标签: c++ visual-c++ g++


【解决方案1】:

是的,POD 类型可以有一个显式声明的默认移动赋值运算符: https://en.cppreference.com/w/cpp/named_req/PODType

这是 MSVC 中的一个错误,直到编译器版本 19.26 才出现,并在 19.27 中修复。演示:https://gcc.godbolt.org/z/c1o5fds3x

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 2018-04-10
    • 2018-10-13
    • 2013-08-09
    • 1970-01-01
    相关资源
    最近更新 更多