在下面的代码中,我想知道 std::tuple_size<T>{}
到底是怎么回事?和 std::tuple_size<T>()
返回元组的大小。查看 docs 时为了使用这个调用,我似乎要么必须使用 () operator
或静态 ::value
成员变量以获得元组大小。但是,当运行此代码时,它会编译并生成正确的输出,tuple_size
是怎样的?从构造函数返回值?
#include <iostream>
#include <tuple>
template <class T>
void test(T)
{
//std::make_index_sequence<std::tuple_size<T>{}>{} Seen used like this spurned my ?
std::cout << std::tuple_size<T>{} << '\n';
std::cout << std::tuple_size<T>() << '\n';
std::cout << std::tuple_size<T>()() << '\n';
std::cout << std::tuple_size<T>::value << '\n';
}
int main()
{
test(std::make_tuple(1, 2, 3.14));
}
最佳答案
http://en.cppreference.com/w/cpp/utility/tuple/tuple_size
说:
Member constants
value
[static]
sizeof...(Types) (public static member constant)
Member functions
operator std::size_t
converts the object to std::size_t, returns value (public member function)
operator()
(C++14)
returns value (public member function)
难怪。 第一和第二形式使用转换运算符。 第三种形式显式调用函数调用运算符。 第四种形式使用静态成员。 这是一个非常方便的 std 实用程序。没有任何其他标准工具具有如此多的便捷功能。
关于c++ - 元组大小默认构造函数及其到 std::size_t 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49962289/