【问题标题】:Wrapping a Tuple of Vectors in C++ into a Tuple将 C++ 中的向量元组包装成元组
【发布时间】:2014-01-14 14:20:41
【问题描述】:

我试图在 C++ 中将两个向量的元组包装成一个元组,但不能 解码神秘的编译器错误消息。

这里是 sn-p(请原谅我使用 using 指令):

using namespace std;    
make_tuple( tuple< vector<int>, vector<int> >() )

这是 Visual Studio 2010 中的错误消息

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\tuple(127): error C2664: 'std::vector<_Ty>::vector(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'std::tr1::_Nil' to 'const std::allocator<_Ty> &'
          with
          [
              _Ty=int
          ]
          Reason: cannot convert from 'std::tr1::_Nil' to 'const std::allocator<_Ty>'
          with
          [
              _Ty=int
          ]
          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\tuple(127) : see reference to function template instantiation 'std::tr1::_Cons_node<_Car,_Cdr>::_Cons_node<std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&>(_Farg0,_Farg1,_Farg2,_Farg3,_Farg4,_Farg5,_Farg6,_Farg7,_Farg8,_Farg9)' being compiled
          with
          [
              _Car=std::vector<int>,
              _Cdr=std::tr1::_Tuple_type<std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil>::_Type,
              _Farg0=std::tr1::_Nil &,
              _Farg1=std::tr1::_Nil &,
              _Farg2=std::tr1::_Nil &,
              _Farg3=std::tr1::_Nil &,
              _Farg4=std::tr1::_Nil &,
              _Farg5=std::tr1::_Nil &,
              _Farg6=std::tr1::_Nil &,
              _Farg7=std::tr1::_Nil &,
              _Farg8=std::tr1::_Nil &,
              _Farg9=std::tr1::_Nil &
          ]
          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xxtuple0(9) : see reference to function template instantiation 'std::tr1::_Cons_node<_Car,_Cdr>::_Cons_node<const std::vector<_Ty>&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&>(_Farg0,_Farg1,_Farg2,_Farg3,_Farg4,_Farg5,_Farg6,_Farg7,_Farg8,_Farg9)' being compiled
          with
          [
              _Car=std::vector<int>,
              _Cdr=std::tr1::_Cons_node<std::vector<int>,std::tr1::_Tuple_type<std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil>::_Type>,
              _Ty=int,
              _Farg0=const std::vector<int> &,
              _Farg1=std::tr1::_Nil &,
              _Farg2=std::tr1::_Nil &,
              _Farg3=std::tr1::_Nil &,
              _Farg4=std::tr1::_Nil &,
              _Farg5=std::tr1::_Nil &,
              _Farg6=std::tr1::_Nil &,
              _Farg7=std::tr1::_Nil &,
              _Farg8=std::tr1::_Nil &,
              _Farg9=std::tr1::_Nil &
          ]
          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\tuple(86) : see reference to function template instantiation 'std::tr1::tuple<_Arg0,_Arg1>::tuple<const std::vector<_Ty>&>(_Farg0)' being compiled
          with
          [
              _Arg0=std::vector<int>,
              _Arg1=std::vector<int>,
              _Ty=int,
              _Farg0=const std::vector<int> &
          ]
          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\tuple(389) : see reference to function template instantiation 'std::tr1::_Cons_node<_Car,_Cdr>::_Cons_node<_Arg0,std::tr1::_Cons_node<_Arg0,_Cdr>>(const std::tr1::_Cons_node<_Arg0,std::tr1::_Cons_node<_Arg0,_Cdr>> &)' being compiled
          with
          [
              _Car=std::tr1::tuple<std::vector<int>,std::vector<int>>,
              _Cdr=std::tr1::_Tuple_type<std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil>::_Type,
              _Arg0=std::vector<int>
          ]
          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xxtuple1(18) : see reference to function template instantiation 'std::tr1::tuple<_Arg0,_Arg1,_Arg2,_Arg3,_Arg4,_Arg5,_Arg6,_Arg7,_Arg8,_Arg9>::tuple<std::vector<_Ty>,std::vector<_Ty>,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil,std::tr1::_Nil>(std::tr1::tuple<std::vector<_Ty>,std::vector<_Ty>> &)' being compiled
          with
          [
              _Arg0=std::tr1::tuple<std::vector<int>,std::vector<int>>,
              _Arg1=std::tr1::_Nil,
              _Arg2=std::tr1::_Nil,
              _Arg3=std::tr1::_Nil,
              _Arg4=std::tr1::_Nil,
              _Arg5=std::tr1::_Nil,
              _Arg6=std::tr1::_Nil,
              _Arg7=std::tr1::_Nil,
              _Arg8=std::tr1::_Nil,
              _Arg9=std::tr1::_Nil,
              _Ty=int
          ]
          main.cpp(19) : see reference to function template instantiation 'std::tr1::tuple<_Arg0,_Arg1,_Arg2,_Arg3,_Arg4,_Arg5,_Arg6,_Arg7,_Arg8,_Arg9> std::tr1::make_tuple<std::tr1::tuple<std::vector<_Ty>,std::vector<_Ty>>>(_Arg0)' being compiled
          with
          [
              _Arg0=std::tr1::tuple<std::vector<int>,std::vector<int>>,
              _Arg1=std::tr1::_Nil,
              _Arg2=std::tr1::_Nil,
              _Arg3=std::tr1::_Nil,
              _Arg4=std::tr1::_Nil,
              _Arg5=std::tr1::_Nil,
              _Arg6=std::tr1::_Nil,
              _Arg7=std::tr1::_Nil,
              _Arg8=std::tr1::_Nil,
              _Arg9=std::tr1::_Nil,
              _Ty=int
          ]

我的代码有什么问题?

编辑: 如果我明确说明结果类型,那么它可以工作。

make_tuple<tuple<tuple< vector<int>> >>( tuple< vector<int> >() );

编辑: 误报,如果我声明make_tuple 的类型,它也不会编译。

make_tuple<tuple<tuple< vector<int>, vector<int> > >>( tuple< vector<int>, vector<int> >() );

【问题讨论】:

标签: c++ visual-studio-2010 templates compiler-errors


【解决方案1】:

如果我复制该代码 sn-p,我会收到一些奇怪的错误消息

make_tuple( tuple< vector<int>, vector<int> >() );

使用 c++ 4.3.3。但是,如果我调用 make_tuple,因为它应该使用一对向量变量来调用,一切都很好:

vector<int> v1;
vector<int> v2;
tuple< vector<int>, vector<int> > vecvec = make_tuple( v1, v2 );

这也是如此:

int x;
tuple< int, vector<int> > intvec = make_tuple( x, v1 );

【讨论】:

  • "但是如果我调用 make_tuple 因为它应该被调用 ",你就会错过这里的重点。我不是要创建两个向量的元组,而是创建两个向量的元组的元组,这里有 2 层元组 :)
  • 如果您使用单个元组调用 make_tuple 并且 make_tuple 将其转发给元组的构造函数:构造函数将是一个复制构造函数 - 或者我在这里错过了什么?
  • 不,make_tuple 本质上是一个元组构造函数,但您不必显式告诉类型。 make_tuple 接受(理论上)可变数量的参数,并返回一个具有相同数量参数的元组,以及您提供的值。例如:make_tuple(5,4) 等价于std::tuple&lt;int,int&gt;(5,4)make_tuple(make_tuple(5)) 等价于std::tuple&lt;std::tuple&lt;int&gt;&gt; (std::tuple&lt;int&gt;(5))。查看make_tuple的签名。
猜你喜欢
  • 1970-01-01
  • 2015-05-06
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-17
  • 2022-01-14
相关资源
最近更新 更多