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

C++11可变参模板的一个有关问题,编译不过

2012-06-09 
C++11可变参模板的一个问题,编译不过我用GCC4.7(winxp+mingw9.0)编译下面的程序:C/C++ code#includestdli

C++11可变参模板的一个问题,编译不过
我用GCC4.7(winxp+mingw9.0)编译下面的程序:

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


出了一堆编译错误:
D:\mytest>g++ mytemp.cpp -std=c++0x
mytemp.cpp:7:14: 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: 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:11: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: In function 'int main()':
mytemp.cpp:12:19: error: 'printf' was not declared in this scope


[解决办法]
C/C++ code
template<typename T>struct count<T>{    static const int value = 1;};
[解决办法]
-std=c++11

热点排行