2016-08-18 80 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