我的基本想法是从 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)
编译我收到以下错误:
error: no matching function for call to '
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 col 5| note: template argument deduction/substitution failed: main.cpp|104 col 45| note: '
std::tuple<_Elements ...>
' is an ambiguous base class of 'TypeContainer<TypeContainer<float, int>, TypeContainer<>, TypeContainer<int> >
' ||std::cout << std::get<0>(std::get<0>(parms)) << " "
;
似乎在 libg++ 中有人多次从任何似乎是损坏的库的元组类型派生。搜索此主题可将我带到:Empty nested tuples error
这真的相关吗?相同的错误或新错误:-)
最佳答案
不幸的是,您必须添加 get 函数的容器版本:
template <std::size_t I, typename ...T>
decltype(auto) get(TypeContainer<T...>&& v)
{
return std::get<I>(static_cast<std::tuple<T...>&&>(v));
}
template <std::size_t I, typename ...T>
decltype(auto) get(TypeContainer<T...>& v)
{
return std::get<I>(static_cast<std::tuple<T...>&>(v));
}
template <std::size_t I, typename ...T>
decltype(auto) get(TypeContainer<T...> const& v)
{
return std::get<I>(static_cast<std::tuple<T...> const&>(v));
}
并且在 Do
类函数中只使用 get
而不是 std::get
。编译器能够从参数中选择命名空间。
我想,我不确定,这是因为 gcc 有 EBO - Empty Base Optimization - 在其元组中实现。很难猜测的确切原因是什么。您可能会考虑在 gcc bugzilla 中报告此事.
顺便说一句,从 STD 类派生不是好习惯。如果您从组合而不是继承开始,那么您将需要提供自己的 get
函数并且您不会观察到此错误,可能会节省很多时间。
关于c++ - 从 std::tuple 派生时出现混淆,无法处理 std::get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37186443/