C++ 试图使我的枚举等效于std::error\u代码

C++ 试图使我的枚举等效于std::error\u代码,c++,c++11,enums,error-code,C++,C++11,Enums,Error Code,我正在浏览std::error_代码,现在正试图使我的错误枚举列表等效于std::error_代码。我正在遵循教程中的内容,但由于某些原因,我无法使其工作,我总是以错误结束,从“我的项目::我的错误”到“std::错误\u代码”没有可行的转换 这就是我正在处理的问题: #include <system_error> #include <string> namespace std { enum class my_error; template <>

我正在浏览std::error_代码,现在正试图使我的错误枚举列表等效于std::error_代码。我正在遵循教程中的内容,但由于某些原因,我无法使其工作,我总是以错误
结束,从“我的项目::我的错误”到“std::错误\u代码”
没有可行的转换

这就是我正在处理的问题:

#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;
}
#包括
#包括
名称空间标准{
枚举类my_错误;
样板
结构是_error _condition _enum:public true _type{};
}
名称空间my_项目{
枚举类my_错误{
警告=45836431
};
命名空间内部{
结构我的错误类别:公共标准::错误类别{
const char*name()const noexcept override{
返回“AAA”;
}
字符串消息(int ev)常量noexcept覆盖{
const std::string message=“BBB”;
返回消息;
}
};
}
}
内联标准::错误代码生成错误代码(const my_project::my_error&e){
返回{static_cast(e),my_project::internal::my_error_category()};
};
int main()
{
标准::错误代码ec=my_项目::my_错误::警告;
}
这项工作:

#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;
}
#包括
#包括
名称空间my_项目{
枚举类my_错误{
警告=45836431
};
}
名称空间标准{
//这里有一个输入错误,您定义了一个不同的std::my_error类
样板
struct是_error_code_enum:public true_type{};
}
名称空间my_项目{
命名空间内部{
结构我的错误类别:公共标准::错误类别{
const char*name()const noexcept override{
返回“AAA”;
}
字符串消息(int ev)常量noexcept覆盖{
const std::string message=“BBB”;
返回消息;
}
};
}
内联标准::错误代码生成错误代码(const my_project::my_error&e){
返回{static_cast(e),my_project::internal::my_error_category()};
};
}
int main()
{
标准::错误代码ec=my_项目::my_错误::警告;
}

Clang的错误消息让我走上了正确的轨道,因为它告诉我为什么它没有使用正确的构造函数。

您编写了
make\u error\u code()
,但没有使用它。此外,我认为您扩展std命名空间的方式会导致未定义的行为:,@Frank根据教程,我不需要它?也许我看错了。@KjMag教程说没问题?@Frank但是OP在std名称空间中声明了一个类(my_error),而不是模板专门化。至于教程,它们在std名称空间之外声明它们的类。