如何在保留弃用警告的同时删除 类

How to delete classes while keeping deprecation warnings

我正在寻找一种从我的库中删除不推荐使用的 类 的好方法,同时保留不错的错误消息。这个想法是基于我已经用函数做的事情:

namespace
{
    [[deprecated("This function has been replaced by combust()")]]
    void explode() = delete; // Using variadic templates in reality to have all signatures covered

    void combust() {}
}

int main()
{
    explode();
    combust();
}

在 clang 中,这给了我一个很好的错误消息:

<source>:11:2: error: call to deleted function 'explode': This function has been replaced by combust()
explode();
^~~~~~~

GCC 只给我这个函数已被删除的消息。尽管如此,这仍然表明了所有试图升级库但忽略弃用警告的人的意图。

因此,由于这是一个 c++ 库,我主要有 类 并且正在寻找正确的方法来使用这些 类 做类似的事情。我目前的做法是这样的:

namespace
{
    class [[deprecated("Class has been replaced by Bar")]] Foo
    {
        Foo () = delete; // And every other method I had in this class
    };
    class Bar
    {
    };
}

int main()
{
    Foo f;
    Bar b;
}

这基本上给了我以下 warnings/errors 在 clang 中(在 GCC 中类似):

<source>:13:5: warning: 'Foo' is deprecated: Class has been replaced by Bar [-Wdeprecated-declarations]
Foo f;
^
<source>:3:60: note: 'Foo' has been explicitly marked deprecated here
class [[deprecated("Class has been replaced by Bar")]] Foo
^
<source>:13:9: error: call to deleted constructor of '(anonymous namespace)::Foo'
Foo f;
^
<source>:5:8: note: 'Foo' has been explicitly marked deleted here
Foo () = delete;
^

我可以忍受这些无用的函数代码,因为这是单行代码,这对 类 来说变得很麻烦,因为它们可以有很多方法。

所以,我正在寻找的是执行以下操作的好方法:(非编译代码)

class [[deprecated("Class has been replaced by Bar")]] Foo = delete;

我到达 oneliner 的时间是:

struct [[deprecated("Class has been replaced by Bar")]] Foo { Foo() = delete; };
struct [[deprecated("Class has been replaced by Bar")]] Foo;

请注意,不包括 Foo 通过引用传递和调用某些方法的情况。

有没有人有更好的解决方案来删除 类,同时对以下某些版本有明确的弃用警告?

可以使用 static_assert 执行此操作,因为它会导致编译时错误并显示一条消息。但是,如果放置在非模板函数中,它将始终断言。为了解决这个问题,我们使它成为一个模板函数,断言依赖于参数。如果不使用该函数,这不会导致错误,因为它不会被实例化。

template <int I = 0>
void explode()
{
    static_assert(I && false, "This function has been replaced by combust()");
}

int main()
{
    // error: static_assert failed: "This function has been replaced by combust()"
    explode();
}

这也可以用于 类。但是,由于需要模板参数,因此需要使用 typedef。

namespace
{
    template <int I = 0>
    class FooDeprec
    {
        static_assert(I && false, "Class has been replaced by Bar");

        FooDeprec() = default; // no need to delete
    };

    using Foo = FooDeprec<>;
}

int main()
{
    // error: static_assert failed "Class has been replaced by Bar"
    Foo f;
}

这样做的好处是不需要太多改动——你可以保留成员函数的声明。