3
的[[deprecated]]方法我想标记为我的接口的某些方法已弃用。 为了向后兼容,我需要在一段时间内支持旧方法。无法实现接口
// 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
};
我使用选项款待Wanings视为错误(/ 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++的错误吗? 是否有任何方法可以在Visual Studio中正确使用已弃用声明?
什么是声明一个函数'[已废弃]'然后禁用警告的意义呢? – nwp
http://stackoverflow.com/a/295229/612920 – Mansuro
@nwp,声明是为我的界面的用户。我是接口的提供者。 –