dimanche 2 juin 2019

Template Specialization with Default Value

This is the code I am working with:

#include <iostream>
#include <type_traits>

using namespace std;

using match_type = void;
using match_type_bad = int;

// version A
template <typename T, typename Attempt = match_type>
struct simple_check : false_type {}; 

// version B
template <typename T>
struct simple_check<T, T> : true_type {}; 

int main() {
  cout << simple_check<match_type>::value << endl;
  cout << simple_check<match_type_bad>::value << endl;
}

The program with this template specialization ends up having the output as:

1
0

There is a confusion in my understanding of C++'s tmp as I was assuming that the output should have been 1 1.

My reasoning was:

  1. With simple_check<match_type> it goes into version B and then gets expanded to simple_check<match_type, match_type> which inherits from true_type. So 1 as expected.

  2. Shouldn't the same apply to simple_check<match_type_bad>?

  3. With this logic, any type X into simple_check<X> should always be version B.

It seems that version A's default value of match_type is enforcing some rule when it decides on version B.

Why is version A affecting version B? Possibly something else?

Aucun commentaire:

Enregistrer un commentaire