我有这段代码,但它没有编译:
#include <iostream>
#include <stdexcept>
#include <cassert>
template< class > struct id{};
template< class U, class V> struct base: public id<U>, public id<V>
{
static const bool value = true;
};
template< class U, class V>
struct is_different
{
typedef char (&true_)[1];
typedef char (&false_)[2];
template< class T, class K, bool = base<T,K>::value >
struct checker;
template< class T, class K>
static true_ test( checker<T,K>* );
template< class , class >
static false_ test(...);
static const bool value = sizeof( test<U,V>(0) ) == sizeof(true_);
};
int main (void)
{
bool b1 = is_different<int,float>::value;
bool b2 = is_different<int,int>::value; // <--- error
std::cout << std::boolalpha << b1 << '\n'
<< b2 << '\n';
return 0;
}
错误:
main.cpp: In instantiation of ‘struct base<int, int>’:
main.cpp:25:17: required by substitution of ‘template<class T, class K> static char (& is_different<U, V>::test(is_different<U, V>::checker<T, K>*))[1] [with T = T; K = K; U = int; V = int] [with T = int; K = int]’
main.cpp:31:41: required from ‘const bool is_different<int, int>::value’
main.cpp:39:38: required from here
main.cpp:7:36: error: duplicate base type ‘id<int>’ invalid
template< class U, class V> struct base: public id<U>, public id<V>
为什么我不能将重复继承的失败用作 SFINAE?