代码之家  ›  专栏  ›  技术社区  ›  Yucel_K

在模板初始化过程中,模板参数列表中的false的计算结果是什么[[副本]

  •  1
  • Yucel_K  · 技术社区  · 7 年前

    this tutorial 基本上是有道理的。但我不知道代码的关键部分在哪里。

    一秒钟之内 enable_if false 和模板类型 T 争论。

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

    为什么 这里用的是什么?当代码中遇到模板实例化时,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 回复  |  直到 7 年前
        1
  •  4
  •   NathanOliver    7 年前
    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.
    };
    

    使用和 type void 因为你没有详细说明

    typename enable_if<!is_container<T>::value>::type
    

    现在,如果 false

    模板<等级T>
    结构启用\u if<错误,T>
    

    是 启用\u if bool 模板的一部分是 假 类型 成员。这意味着

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