删除显式默认函数声明时的警告
Warning when an explicitly defaulted function declaration is deleted
是否有诊断标志或工具可以警告我是否有编译器删除的显式默认函数声明?
如果没有,那为什么?删除默认成员是否是期望的行为?这种情况何时以及多久发生一次?
详
我使用的是 clang 版本 5.0.1,但通过最近的 MSVC 或 gcc 版本发出警告也可以。
我所拥有的一个简化的例子:
class NotMoveA
{
public:
explicit NotMoveA(Foo f);
~NotMoveA() = default;
NotMoveA(const NotMoveA &) = delete;
NotMoveA(NotMoveA &&other) = default;
NotMoveA &operator=(const NotMoveA &) = delete;
//will B deleted w/o warning:
NotMoveA &operator=(NotMoveA &&other) = default;
// ...
private:
const std::string badDataMemberDisallowingMoveAssignment;
// ...
}
并在std::vector
中使用NotMoveA
.由于NotMoveA
不是可移动的,我遇到了一些错误,我花了很长时间才弄清楚其原因。直接针对原因(即已删除= default
功能(发出警告会有所帮助。
您需要做的是将默认成员的定义移出类:
class NotMoveA
{
public:
NotMoveA() = default;
~NotMoveA() = default;
NotMoveA(const NotMoveA &) = delete;
NotMoveA(NotMoveA &&other) = default;
NotMoveA &operator=(const NotMoveA &) = delete;
//will B deleted w/o warning:
NotMoveA &operator=(NotMoveA &&other);
// ...
private:
const std::string badDataMemberDisallowingMoveAssignment;
// ...
};
NotMoveA & NotMoveA::operator=(NotMoveA &&other) = default;
一旦你把它变成一个行外定义,那么你将得到一个编译器错误,因为你无法通过= default
定义成员函数,如果它被删除:
错误:默认此移动分配运算符将在以下时间后将其删除 它的第一个 声明 NotMoveA & NotMoveA::operator=(NotMoveA &&other( = default;
相关文章:
- 警告:忽略模板参数上的属性..在 std::unique_ptr (-wignore 属性)的声明中
- C++ 警告:在一个声明中将截断从双精度初始化为浮点数,但在另一个声明中则不初始化
- 删除显式默认函数声明时的警告
- 警告定义朋友操作员在名称空间内声明
- 警告:“名称”声明会遮盖以前的本地
- 海湾合作委员会警告"没有声明任何东西
- 如何处理"警告:在参数声明中使用'auto'仅适用于 -fconcepts"
- 声明多字符字符的警告和错误
- 当外部变量在其他文件中声明不同时,如何使G 产生警告
- 为什么当对象std ::向量被声明但从未使用时,编译器不会发出警告
- ioroutin.c:67:4:警告:函数 ' ' 的隐式声明在 C99 中无效
- 了解初始化和声明指针变量时的警告和编译错误
- 在可疑的情况下发出叮当警告:函数'foo'可以用属性"noreturn"声明吗?
- 禁止显示编译器警告 声明从未引用的函数
- C++ 显式声明会在默认构造函数中触发警告
- GCC 警告初始化顺序与声明顺序不匹配
- 警告:忽略"int scanf(const char*, ...)"的返回值,使用属性 warn_unused_result [-Wunused-result] 声明
- GCC警告已声明但未定义的变量
- C++警告:没有用于声明变量的链接的匿名类型
- 为什么 g++ 链接器不警告这种不一致的函数声明?