constexpr 上下文中运算符 && 的奇怪行为

gla*_*des 4 c++ tuples and-operator if-constexpr

以下代码尝试根据参数包中传递的最后一个参数做出编译时决策。如果参数包参数的数量 > 0,它包含一个比较,然后尝试获取它的最后一个元素。但是,构造的元组是在无效索引处访问的,该索引据称大于最大元组索引(如图所示static_assert)。
如果我这样做怎么可能cnt-1

演示

#include <cstdio>
#include <concepts>
#include <utility>
#include <tuple>


template <typename... Args>
auto foo(Args&&... args)
{
    auto tuple = std::forward_as_tuple(std::forward<Args>(args)...);
    constexpr std::size_t cnt = sizeof...(Args);

    if constexpr (cnt > 0 && std::same_as<std::remove_cvref_t<std::tuple_element_t<cnt-1, decltype(tuple)>>, int>) {
        printf("last is int\n");
    } else {
        printf("last is not int\n");
    }
}

int main()
{
    foo(2);

    foo();
}
Run Code Online (Sandbox Code Playgroud)

错误:

/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/tuple: In instantiation of 'struct std::tuple_element<18446744073709551615, std::tuple<> >':
<source>:13:25:   required from 'auto foo(Args&& ...) [with Args = {}]'
<source>:24:8:   required from here
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/tuple:1357:25: error: static assertion failed: tuple index must be in range
 1357 |       static_assert(__i < sizeof...(_Types), "tuple index must be in range");
      |                     ~~~~^~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/tuple:1357:25: note: the comparison reduces to '(18446744073709551615 < 0)'
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/tuple:1359:13: error: no type named 'type' in 'struct std::_Nth_type<18446744073709551615>'
 1359 |       using type = typename _Nth_type<__i, _Types...>::type;
      |             ^~~~
Run Code Online (Sandbox Code Playgroud)

Hol*_*Cat 6

阻止 rhs 被评估(在运行时计算其值)的短路并不能阻止它被实例化(将模板参数替换为模板,检查它们的有效性,所有这些都在编译时进行)。

我没有看到它不能按您期望的方式工作的任何特殊原因,它只是还没有添加到语言中。

正如@wohlstad 所说,if constexpr解决方案是:

if constexpr (cnt > 0)
{
    if constexpr (std::same_as<std::remove_cvref_t<std::tuple_element_t<cnt - 1, decltype(tuple)>>, int>)
    {
        ...

Run Code Online (Sandbox Code Playgroud)

第一个if 必须是 constexpr,而第二个应该(在您的场景中)。