我想知道,引入std::bool_constant及其随后用于std::true_type和std::false_type的基本原理是什么(以及header <ratio>,cf中定义的比较结构)。( N4389)在C++17?
到目前为止,我只找到了包含以下内容的文件:
虽然这两篇论文都提到了一个“基本原理”-- bug.cgi?id=51 --链接--评论提要--主要是说这是“基于对c++std*的讨论”(大概是指私有反射器?)没有进一步的细节。
以下是文档:constant
发布于 2015-06-03 17:32:01
这是纯粹的句法糖。通常,我们会像这样使用例如标签分发:
void foo_impl(std::false_type) { /*Implementation for stuff (and char) */}
void foo_impl(std::true_type ) { /*Implementation for integers but not char*/}
template <typename T>
void foo(T) {
foo_impl(t, std::bool_constant<std::is_integral<T>{} && !std::is_same<char, T>{}>());
}如果没有bool_constant,我们必须使用更长的类型说明符来指定所需的类型:std::integral_constant<bool, ...>。由于对布尔值使用integral_constant的情况特别频繁,因此需要一种简洁而简短的专门化处理方法,而bool_constant提供了这一点。
实际上,bool_constant不过是bool-specializations of integral_constant的别名模板。
template <bool B>
using bool_constant = integral_constant<bool, B>;更改true_type和false_type的声明(以及integral_constant<bool, ..>的其他用途)的唯一原因是为了简化标准,甚至没有技术需要,因为integral_constant<bool, false>和bool_constant<false>指定的类型完全相同。
https://stackoverflow.com/questions/30626690
复制相似问题