加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 参数包混淆

发布时间:2020-12-16 05:03:51 所属栏目:百科 来源:网络整理
导读:我在写一个类似C 11 std :: tuple的类并尝试用g -4.7编译它时遇到了一个非常奇怪的情况.我基本上需要的是一个包裹类型的元组.我写了这样的东西: #include tuple template class T struct Wrapper { T x; }; template class... Types using Tuple = std::tup
我在写一个类似C 11 std :: tuple的类并尝试用g -4.7编译它时遇到了一个非常奇怪的情况.我基本上需要的是一个包裹类型的元组.我写了这样的东西:
#include <tuple> 

template <class T> 
struct Wrapper { T x; }; 

template <class... Types> 
using Tuple = std::tuple<Wrapper<Types>...>; 

template <class... Types> 
struct X 
{ 
    using MyTuple = Tuple<Types...>; 
}; 

int main( int argc,char** argv ) 
{ 
    // Tuple<int,int> t;  // (1)
    using Y = X<int,int>;
    Y y;                  // (2)
    return 0; 
}

我做了以下观察:

>代码无法编译:
>如果我添加(1),它确实编译.
>如果我删除(1)和(2),它也会编译.

1的错误消息:

test.cpp: In instantiation of ‘struct X<int,int>’:
test.cpp:22:4:   required from here
test.cpp:10:44: error: wrong number of template arguments (2,should be 1)
test.cpp:4:8: error: provided for ‘template<class T> struct Wrapper’

问题:在我看来,上面的代码是正确的,但这是我第一次真正使用参数包.有没有什么理由认为g -4.7不喜欢我的代码,除非它是一个实验性的实现?

解决方法

这很可能是固定在g 4.8中的 bug in g++ 4.7. Ideone(使用g 4.7.2,我无法链接到而不复制你的代码示例,argh)给出了你提到的错误,而 Coliru(使用g 4.8)编译时没有错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读