在什么意义上 std::disjunction 在 compile_time 处短路

In what sense is std::disjunction short-circuiting at compile_time

cppreference.com上的描述来看,我的印象是std::disjunction是为了让我在编译时短路,所以我可以这样使用它:

#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;
    }
}

但是,这不会编译,std::disjunction 不会短路,因为上面的 static_assert 不会触发 (live example)。

但是那短路有什么意义呢?这不可能是||的惯常行为在 运行 时,因为必须在编译时知道 std::disjunction 的类型,这取决于它的值。

您在链接到的页面上找到了解释:

Disjunction is short-circuiting: if there is a template type argument Bi with bool(Bi::value) != false, then instantiating disjunction<B1, ..., BN>::value does not require the instantiation of Bj::value for j > i

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

std::disjunction_v<std::true_type, std::bool_constant<does_not_compile()>>

编译器仍然需要实例化 std::bool_constant<does_not_compile()> 以便它知道整个 std::disjunction<…> 结果是什么(正如您自己注意到的那样)。保证的是不会实例化std::bool_constant<does_not_compile()>::value