samedi 29 octobre 2016

friend template struct declaration with overloads

Considering the following snippet:

namespace nr {

  template <class ...> using void_t = void;

  template <typename T, typename = void> struct type_id {
    static int const value = 0;
  };
  template <typename T> struct type_id<T, void_t<decltype(T::type_id)>> {
    static int const value = T::type_id;
  };

}

Now I want to make the overload be able to access a private type_id static field. I can not find the correct syntax or whether it is even possible. My attempt below gives 0 instead of the exected 1. You do get 1 if you declare the type_id = 1 member public.

class Foo {
  template <typename T, typename> friend struct nr::type_id;
  static const int type_id = 1;
};

int main() {
  std::cout << nr::type_id<Foo>::value << std::endl;
}

Is this possible, and if so, how?

Aucun commentaire:

Enregistrer un commentaire