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:
-
With
simple_check<match_type>
it goes into version B and then gets expanded tosimple_check<match_type, match_type>
which inherits fromtrue_type
. So1
as expected. -
Shouldn't the same apply to
simple_check<match_type_bad>
? -
With this logic, any type
X
intosimple_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