jeudi 2 juin 2016

C++11: "using“ can declare a template deduction, but direct type usage cannot?

As described,I tried variadic template to implement a summation function, like this:

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; 
} 

It outputs 13,no problem. But if I change "using" to a direct type declaration like this:

template<class Head, class... Tail> 
Head sum(Tail&&... value) 
{ 
    Head sum=0; 
    int []{ (sum+=value, 0)... }; 
    return sum; 
} 

gcc compiler will report problem: error: expected unqualified-id before '[' token|

Why? when use "using" to alias a type name, everything is OK, but using the wrapped type directly, will cause error? What does "using" really indicate, and how "using" differentiate the type information it represents?

Thanks very much!

Aucun commentaire:

Enregistrer un commentaire