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::析取短路在什么意义上_C++_Std_C++17_Variadic Templates_If Constexpr - Fatal编程技术网

C++ 编译时std::析取短路在什么意义上

C++ 编译时std::析取短路在什么意义上,c++,std,c++17,variadic-templates,if-constexpr,C++,Std,C++17,Variadic Templates,If Constexpr,从上面的描述中,我的印象是std::disconction设计用于在编译时对我进行短路,因此我可以这样使用它: #include <type_traits> #include <iostream> template<nullptr_t null = nullptr> constexpr bool does_not_compile() { static_assert(null != nullptr); return false; } void

从上面的描述中,我的印象是std::disconction设计用于在编译时对我进行短路,因此我可以这样使用它:

#include <type_traits>
#include <iostream>

template<nullptr_t null = nullptr>
constexpr bool does_not_compile() {
    static_assert(null != nullptr);
    return false;
}

void hello_world () {
    if constexpr (std::disjunction_v<std::true_type, std::bool_constant<does_not_compile()>>) {
        std::cout << "Hello World!" << std::endl;
    }
}
#包括
#包括
模板
constexpr bool不编译(){
静态断言(null!=nullptr);
返回false;
}
void hello_world(){
if constexpr(标准:析取){

std::cout您可以在链接到的页面上找到解释:

析取是短路:如果有一个模板类型参数
Bi
带有
bool(Bi::value)!=false
,那么实例化
析取::value
不需要对j>i实例化
Bj::value

短路行为涉及每个参数类型的
成员,而不是参数类型本身。在不知道其参数的情况下,无法实例化模板。使用
std::disjunction
通常需要实例化。在您的示例中

std::disjunction_v<std::true_type, std::bool_constant<does_not_compile()>>
标准:析取

编译器仍然必须实例化
std::bool_常量
,以便知道整个
std::disjunction
的结果(正如您自己所指出的)。可以保证的是,它不会实例化
std::bool_constant::value

啊,我想我明白了。我做了另一个现场演示来观察这一点:这是编译的,如果你将std::true_类型更改为std::false_类型,那么静态_断言会触发。@x432ph是的,正是这样。现在通过对静态_断言和如果是constexpr,我观察到它们之间非常奇怪的交互。例如,一个不可访问的static_assert(false)会产生一个编译错误,但是重新格式化的方式使编译器不知道它在每个实例化中都是false,然后它就可以编译了。