如何编写一个错误结构,该结构可以包含不同的强键枚举作为错误代码
How to write an error struct which can contain different strongly typed enums as the error code?
我想编写一个简单的错误struct,其中包含某种错误代码和std::string
作为消息。错误代码可能是一个简单的INT,也可以是任何强烈键入的枚举,例如:
enum class NetworkErrorCode {
OK = 0,
CONNECTION_FAILURE,
EMPTY_RESPONSE,
HOST_RESOLUTION_FAILURE,
UNKNOWN_ERROR = 1000,
};
我可以编写一个结构,该结构对每个不同的枚举具有不同的过载构造函数,它可能包含将它们转换为int
以存储它们。但是在这种情况下,我会失去将错误代码与以后的实际枚举值进行比较的可能性。我还需要为每种可能的类型编写一个比较操作员。
我也可以使用这样的模板:
template<typename T>
struct Error
{
T const code;
std::string const message;
};
但是现在我将无法创建这样的错误:
Error{NetworkErrorCode::CONNECTION_FAILURE, "some message"}
。
我需要写:
Error<NetworkErrorCode>{NetworkErrorCode::CONNECTION_FAILURE, "some message"}
。
是否有一种方法可以避免像第一个解决方案一样避免编写大量锅炉板代码,同时仍然可以在不明确指定类型的情况下创建它们的可能性?我听说过C 17中类模板构造函数的模板参数扣除,但我无法绕过它。
using error_code = std::variant<int, NetworkErrorCode, AnotherErrorCode /*etc*/>;
struct Error {
error_code code;
std::string message;
};
这要求您在一个位置列出所有错误代码类型;但这比编写要写的构造函数要少。
Error{NetworkErrorCode::CONNECTION_FAILURE, "some message"}
在不编写其他构造函数的情况下工作,因为变体将从其任何可能的类型中转换。
要获得访问权限,您拥有std::visit
或.get<Type>()
之类的。error_code
上的==
通常应该只能使用(有时您可能必须将另一个参数明确地施加给error_code
(。
您需要做的就是添加一个ctor,如以下
Error(T const t, const string s) : code(t), message(s) {}
然后使用C 17符合编译器,您将全部设置。
您可以在Godbolt上检查它,https://godbolt.org/g/dyznp8
请注意,这使用C 17模板参数对类模板构造函数扣除而不是使用变体。
此处列出了完整的示例代码,以供您参考,
#include <string>
using namespace std;
enum class NetworkErrorCode {
OK = 0,
CONNECTION_FAILURE,
EMPTY_RESPONSE,
HOST_RESOLUTION_FAILURE,
UNKNOWN_ERROR = 1000
};
template<typename T>
struct Error
{
T const code;
std::string const message;
Error(T const t, const string s) : code(t), message(s) {}
};
template <typename T>
auto getStuff() -> variant<string, Error<T>> {
variant<string, Error<T>> w;
return w;
}
int main() {
int i = 2;
if (i == 1) {
Error{NetworkErrorCode::CONNECTION_FAILURE, "some message"};
}
}
您可以用-std=c++17
进行编译。
- 结构包含在两个头文件中,这两个文件我都不拥有
- C++ 写入路径名中包含不需要的空字符的文件
- 为什么Microsoft在新代码中使用结构而不是类?
- 为什么我应该重载 () 运算符以在结构而不是类中priority_queue?
- ctypes - 结构包含垃圾/不正确的数据
- 无法打开包含文件'Graphics.hpp'没有这样的文件或目录,Visual Studio的其他包含不起作用
- 结构/方法不起作用
- 标头包含不是 .c 程序所必需的,而是.cpp程序需要的
- 无法转换 .CATPart 文件.错误:输入文件路径似乎包含不支持的字符
- 为什么结构对象不是类型?
- 结构包含对自我或其他结构类型的引用
- C :如何访问结构而不会获得SigSEV错误
- 结构程序不起作用
- MPI_Op_create:候选功能不可行.自定义结构指针不能解释为空指针
- C++ - 在类中包含不是类对象属性的变量是否是一种不好的做法
- 函数指针数组,其中包含不同类的类函数
- 使用位域的结构大小不正确
- 可以结构包含参数
- 在文档中引用使用doxygen的结构成员不起作用;结构成员未出现在文档中
- 结构包含在头文件中时将不起作用