It is not allowed by the standard, however you could use one of the following two workarounds to get a similar behaviour.
The first would be to use using
to change the visibility of the method to private, thus preventing others from using it. The problem with that solution is, that calling the method on a pointer of the super-class does not result in a compilation error.
class B
{
public:
virtual void f();
};
class D : public B
{
private:
using B::f;
};
The best solution I have found so far to get a compile-time error when calling D
s method is by using a static_assert
with a generic struct that inherits from false_type
. As long as noone ever calls the method, the struct stays undefied and the static_assert
won't fail.
If the method is called however, the struct is defined and its value is false, so the static_assert
fails.
If the method is not called, but you try to call it on a pointer of the super class, then D
s method is not defined and you get an undefined reference
compilation error.
template <typename T>
struct fail : std::false_type
{
};
class B
{
public:
virtual void f()
{
}
};
class D : public B
{
public:
template<typename T = bool>
void
f()
{
static_assert (fail<T>::value, "Do not use!");
}
};
Another workaround would be to throw an exception when the method is used, but that would only throw up on run-time.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…