- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我使用从 std::system_error
继承的类进行错误处理,我想控制调用 what()
时返回的内容。原因:标准(C++11 和 C++1y CD 草案 - N3690,下面的 § 引用是后者)没有指定 what()
返回的字符串应该是什么样子就像,它只是在 §19.5.6.2 (14) 中给出注释:
Note: The returned NTBS might be the contents of
what_arg + ": " +
. — end note
code.message()
因此它应被视为依赖于实现。 (顺便说一句,不应该是 code().message()
而不是 code.message()
吗?)
所以,问题是:如果我想符合标准并且不依赖于实现(即想要可移植),我该如何精确定义 what()
返回的字符串?
对于那些喜欢代码的人:
class my_class : public std::system_error {
public:
my_class(std::error_code ec, std::string const & what_arg)
: system_error(ec, /* this string is not required to be equal to what is returned by what() */)
{
// ok, try it here
// but what is the name of the member storing the string?
}
const char * what() const noexcept
{
// ok, try it here
// but how to access what_arg in its unaltered form?
}
};
好的,我不喜欢的一个简单的解决方案如下:
class my_class : public std::system_error {
std::string my_what;
public:
my_class(std::error_code ec, std::string const & what_arg)
: system_error(ec, what_arg),
my_what( /* construct my what string */ )
{ }
const char * what() const noexcept
{ return my_what.c_str(); }
};
因为 std::exception::what()
是虚拟的,所以它可以工作,但是有没有更优雅的方式而不使用任何实现细节?我不喜欢存储两个字符串的想法:一个在 std::system_error
中,另一个在 my_what
中。
问题的根源:std::runtime_error——恰好是 std::system_error 的父类——在 §1.9.2.6 (3) 中有一个确切的要求,构造函数的后置条件:
strcmp(what(), what_arg.c_str()) == 0
其中,在 std::system_error
的情况下,在 §19.5.6.2 (2) 中变为以下内容:
string(what()).find(what_arg) != string::npos
有人知道为什么标准如此努力地将 code().message()
包含到 what()
中吗?请注意,code()
返回错误代码对象,因此任何人都可以随时在字符串中包含 code().message()
(即使在出现异常时)这个类的被捕获了)。
如果 std::system_error
的要求与 std::runtime_error
的要求相同,我可以这样写:
class my_class : public std::system_error {
public:
my_class(std::error_code ec, std::string const & what_arg)
: system_error(ec, /* build my what string here */ )
{ }
};
有没有优雅便携的解决方案?
更新:下面的许多评论都指出错误消息是实现定义的。我明白,我只是想格式化 what()
返回的字符串,我不想让它在所有系统上逐字节等效。想想我想记录它或将它传递给第 3 方,它应该遵循某种固定格式(这不是标准建议的格式)。
更新 2:我相信 std::system_error 不仅仅针对操作系统或 STL 错误。我可以(并且假设)从中派生出我自己的类并将它们用于错误报告。如果我正在编写低级 API 怎么办?顺便问一下,为什么禁止在高级 API 中使用它?
如果我在我的 API 的错误处理部分将所有参数传递给它的构造函数,则没有没有实现定义(即未知)涉及错误字符串,但我仍然无法在不复制的情况下格式化它数据。
最佳答案
我不知道“优雅且便携”的解决方案,但我认为您在问题中提出的解决方案没有任何问题,即制作您自己的 what_arg
.
异常对象不会持续很长时间:通常,只够展开堆栈。此外,what_arg
中的字符串不太可能很长,因为 1 兆字节的 what
可读性不佳。最后,异常对象只会在特殊情况下创建,因此“不必要”的小字符串重复不会对程序性能产生任何明显影响。
基本上,类设计者已选择使类的状态不透明,并且 - 坦率地说 - 您不相信他们会从该状态产生可用的结果(可读消息)。在这种情况下,您将不得不复制状态。这是状态封装的常见结果,一般的解决方案几乎总是复制状态。在这种情况下,成本很小,因此如果控制 what()
的值对您很重要,您应该接受成本并继续前进。
关于c++ - 在不复制数据的情况下,决定 what() 从继承自 std::system_error 的类返回什么的符合标准的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17458421/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!