mercredi 17 août 2016

how is template class matched

I try to understand the enable_if implementation, which is a pair of template classes. what I do not understand, why enable_if<true, int> is not matched to the first one? how is this decided?

#include <iostream>

template <bool, class T = void>
struct enable_if
{
    enable_if() { std::cout << "invalid type";}
};

template <class T>
struct enable_if<true, T>
{
  typedef T type;
  enable_if() { std::cout <<"valid type";}
};

int main(){
    enable_if<0==0, int> example; // print "valid type"
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire