C++ 解决GCC 5.5元组初始化错误的方法
考虑以下代码:C++ 解决GCC 5.5元组初始化错误的方法,c++,gcc,C++,Gcc,考虑以下代码: std::vector< std::tuple<std::vector<std::size_t>, std::vector<std::size_t>, std::vector<std::size_t>> > foo = { {{2, 1, 2, 3}, {1, 2}, {2,
std::vector<
std::tuple<std::vector<std::size_t>,
std::vector<std::size_t>,
std::vector<std::size_t>>
> foo = {
{{2, 1, 2, 3}, {1, 2}, {2, 3}},
{{2, 3, 4, 0}, {3}, {2, 3, 4}},
{{2, 3, 4, 0}, {0}, {3, 4, 0}},
};
std::vector<
std::tuple
>foo={
{{2, 1, 2, 3}, {1, 2}, {2, 3}},
{{2, 3, 4, 0}, {3}, {2, 3, 4}},
{{2, 3, 4, 0}, {0}, {3, 4, 0}},
};
在Clang和GCC6或更高版本中,它编译得很好。GCC 5.5中给出了以下错误:
In function 'int main()':
:16:4: error: converting to
'std::tuple<std::vector<long unsigned int, std::allocator<long unsigned int> >,
std::vector<long unsigned int, std::allocator<long unsigned int> >,
std::vector<long unsigned int, std::allocator<long unsigned int> > >'
from initializer list would use explicit constructor
'constexpr std::tuple< <template-parameter-1-1> >::tuple(const _Elements& ...)
[with _Elements = {
std::vector<long unsigned int, std::allocator<long unsigned int> >, std::vector<long unsigned int, std::allocator<long unsigned int> >, std::vector<long unsigned int, std::allocator<long unsigned int> >}]'
};
^
函数“int main()”中的:
:16:4:错误:转换为
'std::tuple'
从初始值设定项列表将使用显式构造函数
'constexpr std::tuple<>::tuple(const_元素&…)
[带_元素={
std::vector,std::vector,std::vector}]
};
^
这是为什么?我如何解决它?一种可能的解决方法是显式调用
tuple
构造函数:
using bar = std::tuple<std::vector<std::size_t>,
std::vector<std::size_t>,
std::vector<std::size_t>>;
std::vector<bar> foo = {
bar{{2, 1, 2, 3}, {1, 2}, {2, 3}},
bar{{2, 3, 4, 0}, {3}, {2, 3, 4}},
bar{{2, 3, 4, 0}, {0}, {3, 4, 0}},
};
使用bar=std::tuple;
标准::向量foo={
条{2,1,2,3},{1,2},{2,3},
条{2,3,4,0},{3},{2,3,4},
条{2,3,4,0},{0},{3,4,0},
};
IIRC GCC 5系列默认使用C++11,而GCC 6和7系列默认使用C++14。而且,这些标准之间也有一些变化。这些变化可能与你的问题有关。@Someprogrammerdude显然。。这是C++17的一个特性。我猜GCC6中添加了对它的支持。看起来这可能适合您:您可以通过显式调用所有构造函数来解决这个问题。