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:
- why need line 1 ?
- 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
Post a Comment