c++ - 创建一个伪元组,一个存储在别处的数据的前端

标签 c++ templates data-structures stl compiler-errors

我正在尝试使用处理元组的数据结构,但是我需要制作一个真正从全局数据结构中提取数据的“元组”,根据值的索引获取数据。到目前为止,通过这个简单的设置,它运行良好:

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/

相关文章:

c++ - Obj-C++ 头文件中的 AppKit 对象?

c++ - NoFieldsClass::operator new(std::size_t):标准是否允许它在每次调用时返回相同的地址?

c++ - 算术操作数类型转换

wordpress - 从 Woocommerce 中的单个产品页面中删除产品尺寸

c# - 编写一个函数来比较两个字符串并返回第三个字符串,该字符串仅包含出现在两个字符串中的字母

mysql - 带有 Access 前端的mysql数据库的结构

c++ - 在 C++ 中将类变量传递给异常

asp.net - 使用 itext (itextsharp) 替换一个 PDF 模板页面上的多个不同图像

c++ - 如果存在同名的不相关全局模板函数,为什么不需要模板关键字?

c - 不按唯一键搜索哈希表