c++ - Different template syntax for finding if argument is a class or not -


while reading this question , came across @johannes's answer.

template<typename> struct void_ { typedef void type; };  template<typename t, typename = void>  // line 1 struct is_class { static bool const value = false; };  template<typename t> struct is_class<t, typename void_<int t::*>::type> { // line 2   static bool const value = true;  }; 

this construct finds if given type class or not. puzzles me new kind of syntax writing small meta program. can explain in detail:

  1. why need line 1 ?
  2. what meaning of syntax <int t::*> template parameter in line 2 ?

line 1: choosing partial specialization below if test succeeds.

line 2: int t::* valid if t class type, denotes member pointer.

as such, if valid, void_<t>::type yields void, having partial specialization chosen instantiation value of true. if t not of class type, partial specialization out of picture sfinae , defaults general template value of false.

everytime see t::something, if something isn't present, type, data member or simple pointer definition, got sfinae going.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -