试图让我的枚举等同于 std::error_code
Trying to make my enum equivalent to std::error_code
我是 going through std::error_code and am now trying to make my list of error enums equivalent to std::error_code. I'm following what's on this 教程,但由于某种原因我无法让它工作,我总是以错误 No viable conversion from 'my_project::my_error' to 'std::error_code'
.
结束
这就是我正在使用的:
#include <system_error>
#include <string>
namespace std {
enum class my_error;
template <>
struct is_error_condition_enum<my_error> : public true_type {};
}
namespace my_project {
enum class my_error {
warning = 45836431
};
namespace internal {
struct my_error_category : public std::error_category {
const char* name() const noexcept override {
return "AAA";
}
std::string message(int ev) const noexcept override {
const std::string message = "BBB";
return message;
}
};
}
}
inline std::error_code make_error_code(const my_project::my_error &e) {
return {static_cast<int>(e), my_project::internal::my_error_category()};
};
int main()
{
std::error_code ec = my_project::my_error::warning;
}
这个有效:
#include <system_error>
#include <string>
namespace my_project {
enum class my_error {
warning = 45836431
};
}
namespace std {
// you had a typo here, and you defined a different std::my_error class
template <>
struct is_error_code_enum<my_project::my_error> : public true_type {};
}
namespace my_project {
namespace internal {
struct my_error_category : public std::error_category {
const char* name() const noexcept override {
return "AAA";
}
std::string message(int ev) const noexcept override {
const std::string message = "BBB";
return message;
}
};
}
inline std::error_code make_error_code(const my_project::my_error &e) {
return {static_cast<int>(e), my_project::internal::my_error_category()};
};
}
int main()
{
std::error_code ec = my_project::my_error::warning;
}
Clang 的错误消息让我走上了正确的轨道,因为它告诉我为什么它没有使用正确的构造函数。
我是 No viable conversion from 'my_project::my_error' to 'std::error_code'
.
这就是我正在使用的:
#include <system_error>
#include <string>
namespace std {
enum class my_error;
template <>
struct is_error_condition_enum<my_error> : public true_type {};
}
namespace my_project {
enum class my_error {
warning = 45836431
};
namespace internal {
struct my_error_category : public std::error_category {
const char* name() const noexcept override {
return "AAA";
}
std::string message(int ev) const noexcept override {
const std::string message = "BBB";
return message;
}
};
}
}
inline std::error_code make_error_code(const my_project::my_error &e) {
return {static_cast<int>(e), my_project::internal::my_error_category()};
};
int main()
{
std::error_code ec = my_project::my_error::warning;
}
这个有效:
#include <system_error>
#include <string>
namespace my_project {
enum class my_error {
warning = 45836431
};
}
namespace std {
// you had a typo here, and you defined a different std::my_error class
template <>
struct is_error_code_enum<my_project::my_error> : public true_type {};
}
namespace my_project {
namespace internal {
struct my_error_category : public std::error_category {
const char* name() const noexcept override {
return "AAA";
}
std::string message(int ev) const noexcept override {
const std::string message = "BBB";
return message;
}
};
}
inline std::error_code make_error_code(const my_project::my_error &e) {
return {static_cast<int>(e), my_project::internal::my_error_category()};
};
}
int main()
{
std::error_code ec = my_project::my_error::warning;
}
Clang 的错误消息让我走上了正确的轨道,因为它告诉我为什么它没有使用正确的构造函数。