模板初始化期间,模板参数列表中的false会被评估为什么?

时间:2018-10-19 19:56:40

标签: c++ templates sfinae

我刚刚开始研究SFINAE,以了解它的含义。我关注的是this tutorial及其意义。但是我很困惑代码的关键部分在哪里。

在第二个enable_if模板参数列表中。有一个false和一个模板类型T参数。

template <class T>
struct enable_if<false, T>
{};

为什么在这里使用false?在代码中遇到模板实例化时,false会导致什么?如果有针对此类模板参数类型的术语,请与我分享。

这是完整的代码-

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

template <typename T>
class is_container
{
  typedef char true_type;
  struct false_type { true_type _[2]; }; //false_type must be larger then the true_type. 
  template <typename U>
  static true_type has_iterator_checker(typename U::iterator *); //overload 1,.called if the type passed contains an iterator.  and returns a true type.
  template <typename U>
  static false_type has_iterator_checker(...); //if false type is passed //overload 2, called if the type passed DOESN'T contain an iterator. and returns a false type.
public:
  enum
  {
    value = (sizeof(has_iterator_checker<T>(0)) == sizeof(true_type)) //compares the size of the overlad with the true type. if they dont match, value becames false.
  };
};

template <bool Cond, class T = void> //first param is is a bool condition, second param is the return type. 
struct enable_if
{
  typedef T type; //if bool Cond is true, it returns the type.
};

//if bool cond is false, appereantly the following template does nothing and compiler silently fails without throwing error.  
//it seems this is what makes SFINAE a powerfull tool.
//but what i dont understand is that. <false, T>. there is no bool type before false. so what does this false evaluate into whenever compiler
//encounters  it the code?
template <class T>
struct enable_if<false, T>
{};

template <typename T>
typename enable_if<!is_container<T>::value>::type
super_print(T const &t)
{
  std::cout << t << std::endl;
}

template <typename T>
typename enable_if<is_container<T>::value>::type
super_print(T const &t)
{
  typedef typename T::value_type value_type;
  std::copy(t.begin(),
    t.end(),
    std::ostream_iterator<value_type>(std::cout, ", "));
  std::cout << std::endl;
}

int main()
{
  super_print(10); // a condition that is false.
  std::vector<int> b;
  b.push_back(1);
  b.push_back(2);
  b.push_back(3);
  super_print(b);

  return 0;
}

1 个答案:

答案 0 :(得分:4)

[array([1, 2, 3], dtype=int64),
 array([1, 2, 3, 4, 5], dtype=int64),
 array([1, 2], dtype=int64)]

template <class T> struct enable_if<false, T> {}; 类的部分专业化知识,它被用作死胡同。当你有

enable_if

编译器评估template <typename T> typename enable_if<!is_container<T>::value>::type super_print(T const &t) { std::cout << t << std::endl; } 。如果是!is_container<T>::value,那么

true

是使用的template <bool Cond, class T = void> //first param is is a bool condition, second param is the return type. struct enable_if { typedef T type; //if bool Cond is true, it returns the type. }; enable_if变成了type,因为您没有在其中指定任何内容

void

现在,如果计算结果为typename enable_if<!is_container<T>::value>::type ,则

false

是选择的template <class T> struct enable_if<false, T> {}; ,因为我们说这是当模板的enable_if部分为bool且没有{{1}时使用的那个}成员。这意味着

false

是替换失败,因为没有type成员。由于替换失败不是错误,因此编译器只是从重载集中删除函数。