c++ - 无法创建其中包含空元组的一元元组 (c++0x)

标签 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>&'

最佳答案

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

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.

我认为这是 std::tuple 实现中的错误来自你的编译器; “备注”暗示不应考虑此构造函数,因为它不会编译。

关于c++ - 无法创建其中包含空元组的一元元组 (c++0x),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5481658/

相关文章:

c++ - 模板友元

c++ - 别名模板的包扩展

c++ - 函数参数前的类关键字是什么?

c# - 从 C# 4.0 中的元组列表中查找和删除元组

C++ 严格弱排序派生类

c++ - QJsonObject 返回 null

c++ - 二分查找的逻辑

c++ - 从不将涉及动态内存分配的函数注释为 noexcept?

mongodb 元组比较(有序)

c++ - 是否允许类型相同但大小不同的双关数组?