- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试寻找一种好方法来从我的库中删除不推荐使用的类,同时保留良好的错误消息。这个想法基于我已经用函数做过的事情:
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/
仅使用POSIX shell 的特性,是否有一个“简单命令”,它什么也不做,也不改变$? 的值。人们通常描述:作为 shell 的无操作命令,但这总是设置 $?为零,所以这不是我想要的。 这是生成 s
我需要保留当前的 GlBlendFunc 以便在我做一些工作后可以恢复它。似乎这不是可以用 GLPushAttrib 保存的属性之一,还有其他类似的方法可以用来保存状态吗? 最佳答案 glGet与
我目前正致力于创建 Fuseki 三重存储浏览器的项目。我需要可视化 TripleStore 中的所有数据并使应用程序可浏览。唯一的问题是 QuerySolution 遗漏了三元组中的“”。 如果我使
我知道没有必要保留委托(delegate),以避免保留循环。我在一次采访中碰巧遇到了一个问题,“如果保留了 appDelegate 会怎样?”。我对此没有答案,并根据我的知识在这里寻求答案。谢谢 最佳
我正在用 C 编写第一个更严肃的程序,但我陷入了困境。我需要将此列表排序为不同的单独文件,因此它看起来像这样: BE30B Berlin 2014-04-02 Gale 02 BE30B Berlin
我有 4 个页面,我使用 ajax 和 historyPopState 在它们之间进行更改。但是有一个问题,因为页面非常不同,它们使用不同的样式表和脚本。我可以一遍又一遍地下载它们,但我想问一下是否有
我有一个表单,其中包含从一个表创建的多行(与其他表没有关系)。当我保存表单时,我所做的每项更改都会保存,但数据库中确实有一个额外的空行。请参阅下文(希望)了解所有必要的信息。 PropertyAddi
我正在编写一个可以在 Canvas 上绘制气泡的应用程序。我有 MainActivity,它的布局是一个简单的 LinearLayout,我将其用作 fragment 的容器。当我在 Canvas 上
我想弄清楚为什么我通过这段代码得到 EXC_BAD_ACESS。我没有线索。谁能帮帮我。 - (void)loadJsonFile:(NSString*)fileName { NSError
我正在编写一个代码,它将遍历单词中的每个单词,在字典中查找它们,然后将字典值附加到计数器。但是,如果我打印计数器,我只会从我的 if 语句中获取最后一个数字(如果有的话)。如果我将 print cou
抱歉,标题有点困惑。 我想隐藏这些选择选项,只在用户选择按钮时显示。一旦用户选择了一个按钮,我希望选择字段将继续显示......但是如果用户选择了不同的按钮,我想从以前的选择中重置所选的选项并炫耀。
>>> t = "first%s\n" >>> t = t %("second") >>> print t firstsecond 无论如何我可以保留最后的“\n”并得到“firstsecond\n”
我试图弄清楚如何更改 if 语句中的变量并使其保持全局不变。 用户输入:!change Hi var A = "Hello" if (msg.content.includes ('!change'))
您好,我有一个关于在重新创建 Activity 时保留 fragment 的问题。我听说一种方法是在 onCreate 方法中使用 setRetainFragment(true) 。问题是——这与跟踪
我想知道是否有人可以帮助我解决下面代码中的内存管理问题。我对 rootController 特别感兴趣,它是在我执行 initWithRootViewController 时被保留还是在窗口 addS
我想确保我在这里正确理解了内存管理。是否有任何特殊原因在这里使用其中一种 assignCurrentDate 方法而不是另一种方法?此外,所有这些都不会导致内存泄漏,对吗? 在 .h 中我们有: NS
我对 Angular2 删除尾部斜杠有疑问。我已经设置了我的 dotnet 核心应用程序来添加它们,但是一旦加载了 js,它们就会被删除。 在 Angular2 中甚至可能吗? (我的客户需要它,所以
local_settings.py 反模式的原因之一是把 SECRET_KEY, AWS设置文件中的键等值有问题: secret 通常应该是这样的: secret !将它们保存在版本控制中意味着拥有存
import('./A'); import('./B'); import('./C'); export class Person {}; A、B 和 C 是纯 JS (es5) 库,它们使用全局 wi
df 是一个测试数据框,其中我只想保留 Hits 列中包含 | 字符的行和行其中包含 hits(即 Hit1、Hit2 等)以及 Hits 列中的空白单元格。 df 从开始 (^) 到结束 ($)
我是一名优秀的程序员,十分优秀!