jeudi 24 août 2017

Trying to make my enum equivalent to std::error_code

I'm still 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 tutorial but for some reason I can't make it work, I always end up with the error No viable conversion from 'my_project::my_error' to 'std::error_code'.

This is what I'm working with:

#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;
}

Aucun commentaire:

Enregistrer un commentaire