不能实现接口的[[deprecated]]方法
Cannot implement [[deprecated]] method of interface
我想将我的接口的某些方法标记为已弃用。为了向后兼容,我需要支持旧方法一段时间。
// my own interface for other
interface I {
[[deprecated( "use 'bar' instead" )]]
virtual void foo() = 0;
};
但是Visual Studio 2015不允许我实现这个接口:
// my own implementation
class IImpl : public I {
public:
virtual void foo() override; // here goes warning C4996:
// 'I::foo': was declared deprecated
};
我使用选项将警告视为错误(/WX),因此无法编译此代码。
我尝试在本地忽略警告:
class IImpl : public I {
public:
#pragma warning(push)
#pragma warning(disable: 4996)
virtual void foo() override;
#pragma warning(pop)
// ... other methods are outside
};
但是没有效果。允许编译代码的唯一解决方案是忽略整个类声明的警告:
#pragma warning(push)
#pragma warning(disable: 4996)
class IImpl : public I {
public:
virtual void foo() override;
// ... other methods are also affected
};
#pragma warning(pop)
GCC似乎使事情变得正确:
#pragma GCC diagnostic error "-Wdeprecated-declarations"
interface I {
[[deprecated]]
virtual void foo() = 0;
};
class IImpl : public I {
public:
virtual void foo() override; // <<----- No problem here
};
int main()
{
std::shared_ptr<I> i( std::make_shared<IImpl>() );
i->foo(); // <<---ERROR: 'virtual void I::foo()' is deprecated [-Werror=deprecated-declarations]
return 0;
}
是msvc++的bug吗?是否有任何方法可以在Visual Studio中正确使用废弃的声明?
标准规定:
实现可以使用不推荐的属性来生成诊断消息程序引用一个名称或实体,而不是声明它
,但IImpl::foo
的声明并不是指I::foo
。
这篇文章内容丰富,不需要严格遵循。实际上,实现可能会警告您它想要的任何东西。但是我仍然认为这是一个bug。
可以这样处理:
// IInternal.h
struct I {
virtual void foo() = 0; // no deprecation
};
// I.h
#include <IInternal.h>
[[deprecated( "use 'bar' instead" )]]
inline void I::foo() {
std::cerr << "pure virtual function I::foo() calledn";
abort();
}
//IImpl.h
#include <IInternal.h>
class IImpl : public I { ... // whatever
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 枚举环境变量的惯用C++14/C++17方法
- 不能实现接口的[[deprecated]]方法