- 论坛徽章:
- 2
|
如题,我采用variadic template来实现一个求和函数,如下所示,没有问题:
- template<class Head, class... Tail>
- Head sum(Tail&&... value)
- {
- Head sum=0;
- using noop=int[];
- noop { (sum+=value, 0)... };
- return sum;
- }
- int main()
- {
- cout<<output<int,int>(1,3,9)<<endl;
- return 0;
- }
复制代码 输出13,没有问题。
如果我把sum函数里面的using noop那行去掉,直接改成内联的声明:
- template<class Head, class... Tail>
- Head sum(Tail&&... value)
- {
- Head sum=0;
- int []{ (sum+=value, 0)... };
- return sum;
- }
复制代码 就有编译错误,除非我给这个数组取个任意的名字
- error: expected unqualified-id before '[' token|
复制代码 为什么用了using,编译就没有问题,using类似于typedef吧。而不用using,就必须有个变量名?
区别在哪里?
谢谢。 |
|