无法创建一个带有空元组的一元元组(c ++ 0x)

时间:2011-03-30 03:51:04

标签: c++ c++11 tuples

我正在尝试使用元组并遇到创建元组的问题。 代码示例如下。

//a.cpp
#include <tuple>
using namespace std;

int main() {
  auto te = make_tuple();    //this line is ok
  auto tte = make_tuple(te); //this line gives an error.
  return 0;
}

我用g ++ 4.5(g ++ -std = c ++ 0x a.cpp)和MS VC ++ 2010编译它。 两个编译器都在main()的第二行给出了一个错误。

我的问题是: 由于'te'是一个定义明确的变量,为什么不能用te作为内容来创建另一个元组。这个语义是否正确?

我想这是一种边界情况,但如果算法正确,则应该允许零,恕我直言。

仅供参考,gcc的错误消息是:

$ gcc -std=c++0x a.cpp

In file included from a.cpp:1:0:
c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/tuple: In constructor
  'std::tuple<_Elements>::tuple(std::tuple<_UElements ...>&) [with _UElements = {},
  _Elements = {std::tuple<>}]':

c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/tuple:551:62:   instantiated from
  'std::tuple<typename std::__decay_and_strip<_Elements>::__type ...> 
  std::make_tuple(_Elements&& ...) [with _Elements = {std::tuple<>&}, typename
  std::__decay_and_strip<_Elements>::__type = <type error>]'
a.cpp:6:27:   instantiated from here

c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/tuple:259:70: error: invalid
  static_cast from type 'std::tuple<>' to type 'const std::_Tuple_impl<0u>&'

1 个答案:

答案 0 :(得分:5)

这看起来好像编译器已将std::tuple<>std::tuple<std::tuple<>>的以下构造函数匹配(参见N3242中的20.4.2p15-17):

  

template <class... UTypes> tuple(const tuple<UTypes...>& u);

     

要求:   sizeof...(Types) == sizeof...(UTypes)。   所有is_constructible<Ti , const Ui &>::value都适用i

     

效果:   构造*this的每个元素   你的相应元素。

     

备注:此构造函数不得   参与重载决议   除非隐含const Ui &   所有Ti都可转换为i

我认为这是编译器实现std::tuple时的错误; “remark”意味着不应该考虑这个构造函数,因为它不会编译。