I want to mark as deprecated some method of my interface. For backward compatibility I need to support old method for some time.
// my own interface for other
interface I {
[[deprecated( "use 'bar' instead" )]]
virtual void foo() = 0;
};
But Visual Studio 2015 don't allow me to implement this interface:
// my own implementation
class IImpl : public I {
public:
virtual void foo() override; // here goes warning C4996:
// 'I::foo': was declared deprecated
};
I use option Treat Wanings as Errors (/WX), so this code can't be compiled.
I try to ignore the warning locally:
class IImpl : public I {
public:
#pragma warning(push)
#pragma warning(disable: 4996)
virtual void foo() override;
#pragma warning(pop)
// ... other methods are outside
};
But it has no effect. The only solution, that allows to compile the code is ignoring the warning for entire class declaration:
#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 seems to make things right:
#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;
}
Is it the bug of MSVC++? Is there any way to use deprecated declaration correctly in Visual Studio?