我正在尝试使用处理元组的数据结构,但是我需要制作一个真正从全局数据结构中提取数据的“元组”,根据值的索引获取数据。到目前为止,通过这个简单的设置,它运行良好:
struct PseudoTuple
{
struct ReturnWrapper
{
//lots of operator overloads and fun stuff for comparisons
}
typedef ReturnWrapper value_type;
//pointers to global data and various info
static const size_t size = 9;
ReturnWrapper operator[](int index)
{
switch (index)
{
//routing to different values
}
}
}
template<> struct std::tr1::tuple_size<PseudoTuple>
{
static const size_t value = PseudoTuple::size;
}
但是,我将这些假元组提供给的数据结构想要使用 std::tr1::get
,而我重载它的尝试失败了:
template<int _Idx> PseudoTuple::ReturnWrapper std::tr1::get(PseudoTuple& pseudotuple)
{
return pseudotuple[_Idx];
}
说它“无法匹配现有的函数声明”。 STL 声明非常困惑,我无法弄清楚问题出在哪里。这是在 Visual C++ 2010 中,以防万一。我担心其他特定于元组的函数也会被调用,但我希望看到如何使 get
工作将告诉我如何使它们也工作(如果需要)。
编辑:ecatmur 建议将函数放在命名空间中:
namespace std
{
namespace tr1
{
template<int _Idx> PseudoTuple::ReturnWrapper get(PseudoTuple& pseudotuple)
{
return pseudotuple[_Idx];
}
}
}
现在我遇到了以前的错误,当时没有合适的 get 函数:
error C2784: '_Arg_traits<_Get<_Idx,tuple<_Arg0,_Arg1,_Arg2,_Arg3,_Arg4,_Arg5,_Arg6,_Arg7,_Arg8,_Arg9>::_MyImpl>::_Type>::_RType std::tr1::get(std::tr1::tuple<_Arg0,_Arg1,_Arg2,_Arg3,_Arg4,_Arg5,_Arg6,_Arg7,_Arg8,_Arg9> &)' : could not deduce template argument for 'std::tr1::tuple<_Arg0,_Arg1,_Arg2,_Arg3,_Arg4,_Arg5,_Arg6,_Arg7,_Arg8,_Arg9> &' from 'const PseudoTuple'
edit 2: 啊,定义的顺序错了,我是在函数使用后定义的。我猜它适用于像 tuple_size
这样的结构,但不适用于函数。
最佳答案
要重载自由函数,您需要在适当的命名空间中提供定义:
namespace std {
namespace tr1 {
template<int _Idx> PseudoTuple::ReturnWrapper get(PseudoTuple& pseudotuple)
{
return pseudotuple[_Idx];
}
template<int _Idx> ... get(const PseudoTuple &pseudotuple)
{
return ...;
}
template<int _Idx> ... get(PseudoTuple &&pseudotuple)
{
return ...;
}
}
}
请注意,根据 17.6.4.2.1 [namespace.std],这是未定义的行为;唯一允许添加到 std
或包含的命名空间的代码对象是用户定义类的模板特化。不过,它应该符合当前标准,因为您的所有参数都是用户定义的类型,并且与标准定义的代码对象没有冲突。
关于c++ - 创建一个伪元组,一个存储在别处的数据的前端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12605654/