大佬教程收集整理的这篇文章主要介绍了c – 如何在保持弃用警告的同时删除类,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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(); }
<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;
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 – 如何在保持弃用警告的同时删除类全部内容,希望文章能够帮你解决c – 如何在保持弃用警告的同时删除类所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。