Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::bool_常数背后的基本原理_C++_C++17 - Fatal编程技术网

C++ std::bool_常数背后的基本原理

C++ std::bool_常数背后的基本原理,c++,c++17,C++,C++17,我想知道,在C++17中引入std::bool_常量及其随后对std::true_类型和std::false_类型(以及在header中定义的比较结构,参见N4389)的基本原理是什么 到目前为止,我只能找到包含以下内容的文件: 虽然这两篇文章都提到了一个“基本原理”——链接到评论提要的大部分内容是“基于对c++std lib*(可能指的是私有反射器?)的讨论”,但没有进一步详细说明 以下是文档: 这是纯语法糖。通常,我们使用标签发送,例如: void foo_impl(std::fal

我想知道,在C++17中引入
std::bool_常量
及其随后对
std::true_类型
std::false_类型
(以及在header
中定义的比较结构,参见N4389)的基本原理是什么

到目前为止,我只能找到包含以下内容的文件:

虽然这两篇文章都提到了一个“基本原理”——链接到评论提要的大部分内容是“基于对c++std lib*(可能指的是私有反射器?)的讨论”,但没有进一步详细说明

以下是文档:

这是纯语法糖。通常,我们使用标签发送,例如:

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>{}>());
}

true\u type
false\u type
(以及
积分常数
的其他用法)的声明被更改的唯一原因是为了标准中的简洁性,甚至:;没有技术上的需要,因为
integral_常量
bool_常量
指定了完全相同的类型。

我的第一个想法也是如此。读一读关于SFINAE的文章,这可能会很有用。@LightnessRacesinOrbit SFINAE和它有什么关系?@Marcadreson:我没能用“SFINAE”来代替一个更好的主意,但这不是一个错误;)@LightnessRacesinOrbit实际上,你能想出一个合理的例子,在这个例子中,
bool_常量
应该与SFINAE一起使用吗?我不能,从我的头顶上。从某种意义上说,它是现代元编程的先驱,而且它(以及
mpl::int \
等等),因此这种技术有一个先例。每当我反复使用
std::integral_constant \/code>时,我都会编写类似的别名;这是一个值得支持的论点。哦,我用
bool\u t
而不是那么长的东西。简洁是最重要的。谢谢你的回答!而且,是的,我已经在使用
std::integral_constant
,所以这个问题有一个隐含的“而不是像以前一样只使用
std::integral_constant
”部分,你也回答了这个问题:语法上的糖分和简洁是有意义的:-)
template <bool B>
using bool_constant = integral_constant<bool, B>;