std :: index_sequence返回大小始终为1
我是C++新手14 std::index_sequence
。该结构的size
成员是什么意思?无论我创建了多少个元素,它总是返回一个。std :: index_sequence返回大小始终为1
std::integer_sequence<int, 1, 2, 3>::size => 1
std::index_sequence<8, 7, 1, 5>::size => 1
虽然返回值应该是3和4(该序列的元素数)。
std::integer_sequence::size
是一个函数,所以你需要调用它:
std::integer_sequence<int, 1, 2, 3>::size() => 3
你可能会看到1
,因为如果你写这样
std::cout << std::integer_sequence<int, 1, 2, 3>::size;
然后代码的函数将被隐式转换为函数指针,然后到bool
,并且值将为true
,因为指针不为空。
我只是好奇。为什么函数指针在cout中被转换为bool? – MayurK
编译器应该警告它。 – SergeyA
@MayurK因为'std :: ostream'和'bool'有一个'operator TartanLlama
[Can not reproduce](http://coliru.stacked-crooked.com/a/07fb9de4c18ea993); [SSCCE](http://sscce.org)需要。 – ildjarn
如果没有[最小,完整和可验证示例](http://*.com/help/mcve)(或SSCCE),很难说什么,但[来自TartanLlama的答案](http:// *。 com/a/40612639/440558)是一个非常好的猜测。这应该给你编译警告。不要忽视编译器警告,它们与错误一样重要。 –