Friend Overload Template Template Announcement

Given 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 overload available for a private static field type_id. I cannot find the correct syntax or even possible. My attempt below gives 0instead of the output 1. You will receive 1if you announce a public member type_id = 1.

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?

+4
source share

Source: https://habr.com/ru/post/1659276/


All Articles