cannot create a unary tuple with an empty tuple in it (c++0x)

Deadly 提交于 2019-12-08 15:50:42

问题


I was experimenting with tuples and encountered a problem with creating tuples. The code example is as follows.

//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;
}

I compiled it with both g++ 4.5 (g++ -std=c++0x a.cpp) and MS VC++2010. Both compilers are giving me an error on the second line in main().

My question is this: Since 'te' is a well-defined variable, why can't another tuple be created with te being the content. Is this semantic correct?

I guess this is kind of a boundary case, but if the arithmetic is correct, zero should be allowed, IMHO.

FYI, the error message from gcc is:

$ 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:


This looks like the compiler has matched your std::tuple<> against the following constructor of std::tuple<std::tuple<>> (See 20.4.2p15-17 in N3242):

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

Requires: sizeof...(Types) == sizeof...(UTypes). is_constructible<Ti , const Ui &>::value is true for all i.

Effects: Constructs each element of *this with the corresponding element of u.

Remark: This constructor shall not participate in overload resolution unless const Ui & is implicitly convertible to Ti for all i.

I think this is a bug in the implementation of std::tuple from your compiler; the "remark" implies that this constructor should not be considered, since it won't compile.



来源:https://stackoverflow.com/questions/5481658/cannot-create-a-unary-tuple-with-an-empty-tuple-in-it-c0x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!