考虑以下代码:
// 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& operator=(A&&) = default;
,两个编译器都一切正常。
那么,POD 类型是否可以具有显式声明的默认移动分配运算符?
是的,POD 类型可以具有显式声明的默认移动分配运算符:https://en.cppreference.com/w/cpp/named_req/PODType
这是 MSVC 中的一个错误,在编译器版本 19.26 中表现出来,并在 19.27 中修复。演示:https://gcc.godbolt.org/z/c1o5fds3x