我的基本想法是从 std::tuple 派生我自己的类,以获取一些辅助类型,如下所示:
template <typename ... T>
class TypeContainer: public std::tuple<T...>
{
public:
using BaseType = std::tuple<T...>;
static const size_t Size = sizeof...(T);
TypeContainer(T... args):std::tuple<T...>(args...){};
using index_sequence = std::index_sequence_for<T...>;
};
现在我尝试使用如下代码:
using MyType_tuple_with_empty = std::tuple< std::tuple<float,int>, std::tuple<>, std::tuple<int>>;
using MyType_typecontainer_with_empty = TypeContainer< TypeContainer<float,int>, TypeContainer<>, TypeContainer<int>>;
using MyType_tuple_non_empty = std::tuple< std::tuple<float,int>, std::tuple<int>, std::tuple<int>>;
using MyType_typecontainer_non_empty = TypeContainer< TypeContainer<float,int>, TypeContainer<int>, TypeContainer<int>>;
template <typename T>
void Do( const T& parms )
{
// The following lines result in errors if TypeContainer with
// empty element comes in. The empty element is in std::get<1> which
// is NOT accessed here!
std::cout << std::get<0>(std::get<0>(parms)) << " ";
std::cout << std::get<1>(std::get<0>(parms)) << std::endl;
std::cout << std::get<0>(std::get<2>(parms)) << std::endl;
}
int main()
{
MyType_tuple_with_empty p1{{ 1.2,3},{},{1}};
Do( p1 );
MyType_typecontainer_with_empty p2{{ 1.2,3},{},{1}};
Do( p2 ); // << this line raise the error
MyType_tuple_non_empty p3{{ 1.2,3},{4},{1}};
Do( p3 );
MyType_typecontainer_non_empty p4{{ 1.2,3},{4},{1}};
Do( p4 );
}
如果我编译Do(p2)
我收到以下错误:
错误:没有匹配的函数可调用 'get(const TypeContainer<TypeContainer<float, int>, TypeContainer<>, TypeContainer<int> >&)
'
有人可以解释为什么存在空的吗TypeContainer
与std::get
会导致那个问题吗?
编辑:
附加信息:
线条
MyType_tuple_with_empty p1{{{ 1.2,3},{},{1}}};
MyType_tuple_non_empty p3{{ 1.2,3},{4},{1}};
不能用gcc5.2.0编译,但用gcc6.1.0编译。这有点神秘,因为我记得元组的构造函数确实是显式的。为什么这适用于 gcc6.1.0?但这不是我寻找的问题:-)
另一个提示:
我遇到问题的代码似乎是用 clang3.5.0 编译的。
有点难以理解...
编辑2:
仔细研究错误列表(很长:-))我发现:
/opt/linux-gnu_5.2.0/include/c++/5.2.0/tuple|832 列 5|注意:模板参数推导/替换失败:
main.cpp|104 第 45 栏|笔记: 'std::tuple<_Elements ...>
' 是 ' 的不明确基类TypeContainer<TypeContainer<float, int>, TypeContainer<>, TypeContainer<int> >
'
||std::cout << std::get<0>(std::get<0>(parms)) << " "
;
似乎在 libg++ 中,有人从任何元组类型中多次派生,这似乎是一个损坏的库。搜索这个主题让我发现:空嵌套元组错误 https://stackoverflow.com/questions/10999062/empty-nested-tuples-error
这真的有关系吗?相同的错误或新的错误:-)