2016-08-18 157 views
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中正确使用已弃用声明?

+0

什么是声明一个函数'[已废弃]'然后禁用警告的意义呢? – nwp

+0

http://stackoverflow.com/a/295229/612920 – Mansuro

+0

@nwp,声明是为我的界面的用户。我是接口的提供者。 –

回答

2

标准表示:

实现可以使用已弃用的属性来产生的情况下,所述 程序指的是名称或其他实体的诊断消息,而不是将它声明

但声明IImpl::foo不是指I::foo

这段文字是资料性的,不需要遵循这封信。事实上,一个实现可能会警告你想要的任何东西。不过,我仍然认为它是一个错误。

它可以这样工作围绕:

// 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() called\n"; 
    abort(); 
} 

//IImpl.h 
#include <IInternal.h> 
class IImpl : public I { ... // whatever