[英]Why does std::initializer_list not support std::get<>, std::tuple_size and std::tuple_element
Why does std::initializer_list
not support std::get<>
, std::tuple_size
and std::tuple_element
? 为什么std::initializer_list
不支持std::get<>
, std::tuple_size
和std::tuple_element
? It is used a lot in constexpr
expressions as is right now, for example, 就像现在一样,它在constexpr
表达式中经常使用,例如,
std::max({1, 2, 3, 4, 5});
And if it did cool things like the following would be possible 如果这样做确实很酷,则可能发生以下情况
auto [x, y] = {1, 2};
So why does std::initializer_list
not support these? 那么为什么std::initializer_list
不支持这些? As far as I know there is no way to construct a std::initializer_list
at runtime, so the size is always fixed by the user. 据我所知,在运行时无法构造std::initializer_list
,因此大小始终由用户确定。
Below is an example of how one can get the size of an std::intializer_list<>
at compile time 下面是一个示例,说明如何在编译时获取std::intializer_list<>
的大小
#include <iostream>
#include <initializer_list>
using std::cout;
using std::endl;
template <typename...> struct WhichType;
template <typename Type>
constexpr int size_init_list(std::initializer_list<Type> il) {
return il.end() - il.begin();
}
int main() {
constexpr auto size = size_init_list({1, 2, 3});
cout << static_cast<int>(std::integral_constant<int, size>{}) << endl;
return 0;
}
Even though the size of a std::initializer_list
is a compile-time constant, the size isn't part of the type. 即使std::initializer_list
的大小是编译时常量,该大小也不是该类型的一部分。 Every initializer list of int
s has the same type, namely std::initializer_list<int>
. 每个int
的初始化列表具有相同的类型,即std::initializer_list<int>
。 And std::tuple_size<std::initializer_list<int>>::value
can only have one possible value. 而且std::tuple_size<std::initializer_list<int>>::value
只能有一个可能的值。 So obviously it cannot be used to get the actual size of an initializer list. 因此很显然,它不能用于获取初始化程序列表的实际大小。 It makes no sense to define it at all. 完全没有定义它是没有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.