c++ - 模板参数列表中的 false 在模板初始化期间评估为什么?
问题描述
我刚刚开始研究 SFINAE 以了解它是什么。我正在关注本教程 ,主要是它是有意义的。但我很困惑代码的关键部分在哪里。
在第二个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;
}
解决方案
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
used 和type
becomevoid
因为你没有指定任何东西
typename enable_if<!is_container<T>::value>::type
现在,如果它评估为false
then
template <class T>
struct enable_if<false, T>
{};
是enable_if
选择的,因为我们说过这是在bool
模板的一部分是false
并且它没有type
成员时使用的那个。这意味着
typename enable_if<!is_container<T>::value>::type
是替换失败,因为没有type
成员。由于替换失败不是错误,编译器只是从重载集中删除 then 函数。
推荐阅读
- symfony - 在我的 Symfony 项目中使用 Webpack encore 进行资产管理
- python - Pyperclip 1.8.2 与 Pyautogui 冲突
- python-3.x - 虚拟麦克风中的音频已饱和
- html - 从 Ilustrator 导入 SVG 不会产生清晰的线条
- apache-spark - Spark 3.1 在 Java 中加入两个数据集
- ruby-on-rails - 为什么 WSL2/Ubuntu 上的密钥可以打开凭证文件,但在另一台机器/服务器上却没有?
- java - 使用 @SpringConfigureMockMvc 和 Hibernate 方言的意外行为
- python - 有没有办法在python中调用多个同名文件来合并一个
- r - R:了解“makePSOCKcluster”的影响
- python - 如何从 URL 而不是文件对图像进行 base64 编码?