Reference:
https://stackoverflow.com/a/70905771
P1306 : Expansion statements
tracking: https://github.com/cplusplus/papers/issues/156
// Returns the nth type in a parameter pack of types (ommited for clarity)
// template <std::size_t N, typename...Ts>
// nth_type{}
template <typename... Ts>
struct Typelist{
template <typename T>
consteval static std::size_t pos() noexcept {
for(std::size_t i{}; i < sizeof...(Ts); ++i) {
using TN = nth_type_t<i, Ts...>;
if (std::is_same_v<T, TN>)
return i;
}
return sizeof...(Ts);
}
};
- Doesn't matter that i is guaranteed to be evaluated only at compile-time when its value is known in an abstract sense.
- Doesn't matter whether the function is consteval or constexpr or none of these.
If i can change in the for loop, that is not possible to guarantee. The language requires that the expression i when used as template argument is by itself a constant expression, independently of whether the whole function body can only be evaluated as part of a larger constant expression.
But i is neither declared constexpr, nor declared const with constant initializer.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.