gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:07:54 25 4
gpt4 key购买 nike

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

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;
}

这基本上给了我以下 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;
}

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

关于c++ - 如何在保留弃用警告的同时删除类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41660746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com