C++ 为什么我能';t使用继承';作为SFINAE的错误是什么?

C++ 为什么我能';t使用继承';作为SFINAE的错误是什么?,c++,multiple-inheritance,sfinae,C++,Multiple Inheritance,Sfinae,我有这段代码,但它不编译: #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 = t

我有这段代码,但它不编译:

#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;       
}
#包括
#包括
#包括
模板struct id{};
模板结构基础:公共id,公共id
{
静态常量布尔值=真;
};
模板
结构是不同的
{
typedef字符(&true)[1];
typedef char(&false))[2];
模板
结构检查器;
模板
静态真实测试(检查程序*);
模板<类,类>
静态假试验(…);
静态常量布尔值=sizeof(测试(0))==sizeof(真);
};
内部主(空)
{
bool b1=不同的::值;

bool b2=is_different::value;//SFINAE仅适用于函数签名中的直接内容。您得到的编译错误发生在
base
的实例化中,这是从函数签名中删除的两个步骤

也就是说,你为什么要这么做

template <typename T, typename U>
struct is_same { static const bool value = false; };

template <typename T>
struct is_same<T, T> { static const bool value = true; };

template <typename T, typename U>
struct is_different { static const bool value = !is_same<T, U>::value; };

现在,
base
实例化并不隐藏在模板默认参数表达式后面,而是在签名中,但您仍然会得到一个错误。

我会说,因为模板参数替换不一定需要实例化模板类,而检测来自同一类型的继承需要实例化模板类。SFINA如果它是它的一部分,那么它将是不一致的。你能展示如上所述的另一个例子吗?即,哪个SFINAE不起作用。对于我来说,不感兴趣的是实现方式是相同的,是不同的还是其他什么,我只感兴趣的是SFINAE规则,我不能使用继承失败。添加了一个彻底的解释。
template <typename T, typename U>
struct is_same { static const bool value = false; };

template <typename T>
struct is_same<T, T> { static const bool value = true; };

template <typename T, typename U>
struct is_different { static const bool value = !is_same<T, U>::value; };
template <typename U, typename V> struct base: public id<U>, public id<V> {
    typedef void type;
};
template <typename U, typename V> struct is_different {
  struct true_ { char c[1]; };  // I don't like sizeof(reference type),
  struct false_ { char c[2]; }; // makes me nervous.
  template <typename T, typename K>
  static true_ test(typename base<T, K>::type*);
  template <typename, typename>
  static false_ test(...);

  static const bool value = sizeof(test<U, V>(0)) == sizeof(true_);
};