c – 如何在保持弃用警告的同时删除类
发布时间:2020-12-16 07:20:21 所属栏目:百科 来源:网络整理
导读:我正在尝试寻找一种从我的库中删除已弃用的类的好方法,同时保留好的错误消息.这个想法是基于我已经完成的功能: namespace{ [[deprecated("This function has been replaced by combust()")]] void explode() = delete; // Using variadic templates in real
我正在尝试寻找一种从我的库中删除已弃用的类的好方法,同时保留好的错误消息.这个想法是基于我已经完成的功能:
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; } 这样做的好处是不需要进行太多更改 – 您可以保留成员函数的声明. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |