samedi 23 février 2019

Accessing different traits specialization of the same trait c++

I'm facing this issue that is driving me crazy. I have a templated structure to be able to provide a partial specialization:

template <typename ReturnType>
struct field {
  template <typename MessateType>
  inline static ReturnType get(MessateType const& message, int const field) {
    static_assert(false, "Missing trait specialization for the given type");
  }
};

Now. I want this generic one to let me know if I'm trying to use it with a non supported type. So far so good.

With this in place, I need to specialize it for int:

template <>
struct field<int> {
  template <typename MessateType>
  inline static int get(MessateType const& message, int const field) {
    return std::atoi(message.getField(field).c_str());
  }
};

And now, apart from some other types that I'm cutting for the sake of brevity, I need to specialize it to boost:optionals. For this, I'm trying the following code:

template <typename T>
struct field<optional<T>> {
  template <typename MessateType>
  inline static optional<T> get(MessateType const& message, int const field) {
    return message.isSetField(field) ? field<T>::get(message, field)
                                     : optional<T>(boost::none);
  }
};

However, I'm getting the following error on the compiler:

error C2275: 'T': illegal use of this type as an expression
error C2039: 'get': is not a member of '`global namespace''

I've been searching for the solution to this, but I'm unable to find it. It looks like the compiler doesn't like the fact that I'm trying to access one of the traits (int) from a different one (optional). I'm clueless about what could this be.

Thanks for reading! :)

EDIT: I corrected the syntax of the optionals. I was using some desperate debugging

Aucun commentaire:

Enregistrer un commentaire