using [[deprecated]]

90 Views Asked by At

I want to deprecate some functions in an old codebase. My header files look like:

foo.h:

class A
{
  [[deprecated]] virtual int F0()=0;
}

bar.h:

class B: A
{
  [[deprecated]] int F0();
}

When I compile this in VS2017, I get a deprecated warning in class B because the name was previously deprecated in class A. If I remove the deprecated directive in B, I also get the warning for the same reason. That seems kind of unfair.

What is the right way around this?

1

There are 1 best solutions below

0
Remy Lebeau On

If a base class method is marked as deprecated, it doesn't matter if an overriding method in a derived class is also marked as deprecated or not. The method is still deprecated in the derived class.

Just like an overriding method doesn't need to use virtual explicitly, but the method is still virtual. The same is true with [[deprecated]], too.

cppreference.com says this:

https://en.cppreference.com/w/cpp/language/attributes/deprecated

A name declared non-deprecated may be redeclared deprecated. A name declared deprecated cannot be un-deprecated by redeclaring it without this attribute.