3

I noticed these strange code from cppreference page. (Run it here).

And I'm puzzled by template <class T> char test(int T::*);(at line 6). What's int T::* meaning? Is it representing a type correctly?

#include <iostream>
//#include <type_traits>

namespace my_impl {
    namespace detail {
        template <class T> char test(int T::*);
        struct two { char c[2]; };
        template <class T> two test(...);
    }

    template <class T>
    struct is_class : std::integral_constant<bool, sizeof(detail::test<T>(0))==1
                                                && !std::is_union<T>::value> {};
}
using my_impl::is_class;

struct A {};

class B {};

enum class C {};

int main() 
{
    std::cout << std::boolalpha;
    std::cout << is_class<A>() << '\n';
    std::cout << is_class<B>() << '\n';
    std::cout << is_class<C>() << '\n';
    std::cout << is_class<int>() << '\n';
}
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
recolic
  • 554
  • 4
  • 18

1 Answers1

1

int T::* is called a member pointer and it is a pointer to an int, but to an int that's a member of the class T. This is only a valid expression, if T is a class.

msc
  • 33,420
  • 29
  • 119
  • 214