首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++11的sizeof.(Args)如何用?编译异常啊

2012-06-09 
C++11的sizeof...(Args)怎么用?编译错误啊!我有下面一段程序,想用sizeof...获取变参的长度,结果编译失败了

C++11的sizeof...(Args)怎么用?编译错误啊!
我有下面一段程序,想用sizeof...获取变参的长度,结果编译失败了:

C/C++ code
#include<stdio.h>template<typename T, typename... Args>struct count{     static const int value = 1 + count<Args...>::value;    static void f(){printf("%d\n",sizeof...(Args));};template<typename T>struct count<T>{    static const int value = 0;};int main(void){   int i=count<int, int, int, int, int>::value;   printf("%d\n",i);   return 0;}


gcc4.7编译:
D:\mytest>g++ mytemp.cpp -std=c++11
mytemp.cpp:7:10: error: declaration of 'class T'
mytemp.cpp:2:10: error: shadows template parm 'class T'
mytemp.cpp:8:8: error: specialization of 'template<class T, class ... Args> struct count' must appear at namespace scope
mytemp.cpp:15:1: error: expected '}' at end of input
mytemp.cpp: In instantiation of 'const int count<int>::value':
mytemp.cpp:4:50: recursively required from 'const int count<int, int, int, int>::value'
mytemp.cpp:4:50: required from 'const int count<int, int, int, int, int>::value'
mytemp.cpp:12:42: required from here
mytemp.cpp:4:50: error: wrong number of template arguments (0, should be 1 or more)
mytemp.cpp:3:8: error: provided for 'template<class T, class ... Args> struct count'
mytemp.cpp:15:1: error: expected unqualified-id at end of input

[解决办法]
把这行
C/C++ code
static void f(){printf("%d\n",sizeof...(Args)); 

热点排行
Bad Request.