This really has nothing to do with whether or not the constexpr
function needs to be able to be runnable at compile-time (tuple_size<T>::value
is a constant expression regardless of whether the T
comes from a constexpr
object or not).
std::get<>
is a function template, that requires an integral constant expression to be called. In this loop:
for (auto i = 0u; i < std::tuple_size<T>::value; ++i)
{
std::get<i>(t);
}
i
is not an integral constant expression. It's not even constant. i
changes throughout the loop and assumes every value from 0
to tuple_size<T>::value
. While it kind of looks like it, this isn't calling a function with different values of i
- this is calling different functions every time. There is no support in the language at the moment for this sort of iteration†, and this is substantively different from the original example, where we're just summing ints.
That is, in one case, we're looping over i
and invoking f(i)
, and in other, we're looping over i
and invoking f<i>()
. The second one has more preconditions on it than the first one.
†If such language support is ever added, probably in the form of a constexpr for
statement, that support would be independent from constexpr
functions anyway.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…