lundi 22 avril 2019

Template non-type parameter with different types

Let's assume an input template parameter T may or may not have internal variable bar. I am trying to write a struct that returns the value of bar when we have it, and returns some constant when we don't. Here is my attempt:

template <typename T, typename std::enable_if<T::hasBar, int>::type>
struct getBar {
  static constexpr unsigned int bar = T::bar;
};

template <typename T, typename std::enable_if<!T::hasBar, int>::type>
struct getBar {
  static constexpr unsigned int bar = 0;
};

I cannot compile this code with C++14. The compiler complains that: "template non-type parameter has a different type". Why we have such an error and how can I address it?

Aucun commentaire:

Enregistrer un commentaire