不能创建一个带有空元组的一元元组(c ++ 0x) [英] cannot create a unary tuple with an empty tuple in it (c++0x)
问题描述
我在尝试元组,遇到创建元组的问题。
代码示例如下。
// a.cpp
#include< tuple>
using namespace std;
int main(){
auto te = make_tuple(); // this line is ok
auto tte = make_tuple(te); //此行给出错误。
return 0;
}
我使用g ++ 4.5(g ++ -std = c ++ 0x a.cpp)和MS VC ++ 2010。
两个编译器都在main()的第二行给我一个错误。
我的问题是:
因为'te'良好定义的变量,为什么不能创建te是内容的另一个元组。这个语义是否正确?
我猜这是一种边界情况,但如果算术正确,应该允许零。
$ b
FYI,来自gcc的错误消息是:
$ gcc -std = c ++ 0x a.cpp
在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:从
实例化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:从这里实例化
c:\mingw\bin\ ../ lib / gcc / mingw32 / 4.5.2 / tuple<>'到类型'const std :: _ Tuple_impl< 0u>&'
上的/ include / c ++ / tuple:259:70:error:invalid
static_cast code>
这看起来像是编译器匹配你的 std ::对于
(见20.4.2p15-17在N3242中): std :: tuple< std :: tuple<>>
的以下构造函数使用元组<>
模板< class ... UTypes> tuple(const tuple< UTypes ...>& u);
需要:
sizeof ...(Types)== sizeof ...(UTypes)
。
is_constructible< Ti,const Ui&> :: value
对所有i
/ p>
效果:
用
构造* this
的每个元素u的相应元素。
注:此构造函数不应
参与重载解析
,除非const Ui&
对于所有i
,隐式地
可转换为Ti
。
< blockquote>
我认为这是一个在你的编译器中执行
std :: tuple
的错误; remark意味着这个构造函数不应该被考虑,因为它不会编译。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>&'
解决方案This looks like the compiler has matched your
std::tuple<>
against the following constructor ofstd::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 alli
.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 toTi
for alli
.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.这篇关于不能创建一个带有空元组的一元元组(c ++ 0x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!