boost::hana tag_of implementation

Viewed 55

I wonder how the when specialization work when there's no case base for the boost::hana::when<false> case.

boost::hana::tag_of implementation:

 template<bool condition>
 struct when; // forward declaration only

 template<typename T, typename = void>
 struct tag_of;

 template<typename T, typename>
 struct tag_of : tag_of<T, when<true> >
 {};

 template<typename T, bool condition>
 struct tag_of<T, when<condition> >
 {
    using type = T;
 };

And a test example:

 struct my_tag {};
 struct my_tag2 {};

 namespace boost {
    namespace hana {

       template<class T>
       struct tag_of<T, when<std::is_same<T, int>{}()> >
       {
           using type = my_tag;
       };

       template<class T>
       struct tag_of<T, when<std::is_same<T, unsigned>{}()> >
       {
          using type = my_tag2;
       };
    }
}

int main()
{
   using type = boost::hana::tag_of<int>::type;
   std::cout << std::is_same<type, my_tag>{} << std::endl;
}

and I wonder why std::is_same<T, int>{}() (or with ::value which is the same), is a more specialized partial specialization than std::is_same<T, unsigned>{}(), and why, if the condition is false for both cases, when<condition> is more specialized.

I have done a lot of metafunctions and work with specializations and parameter packs and the sort, but in this case, there's something that I don't see.

The thing is that I don't see why the true or false value of the when template can matter, if there's no default implementation for the false case.

1 Answers
Related