Re: Help with Code Validation of Variadic templates

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 08/16/09 09:32, Larry Evans wrote:
On 12/07/07 15:12, Rene Buergel wrote:
I'm currently playing around using mainline with the variadic template feature and would like to hear some comments on code gcc is rejecting.

#1:
=====
1 template<typename T, T a, T... Args> T max()
2 {
3     return a > max<T, Args>() ? a : max<T, Args>();
4 };
5
6 template<typename T, T a, T b>T max()
7 {
8     return a > b ? a : b;
9 };

int main()
{
   unsigned int a = max<unsigned int, 3, 2>();
   unsigned int a = max<unsigned int, 3, 2, 5>();
}

=====
Using the non-variadic template, everything works fine. With the variadic templates, gcc issues an error in line 3:
error: parameter packs not expanded with `...':
note:         'Args'
[snip]
I think this is an instance of bug 40092:
[snip]
OOPS.  It's unrealted to bug 40092.
Part of the problem is Args needs to be expanded as follows:

   Args...

I think *maybe* the other problem is template function specialization.
Anyway, the workaround is to use template class specializations
as shown in the attached.

HTH.

-Larry
template<typename T, T... Args>struct maxc
;

template<typename T, T Head>struct maxc<T,Head>
{
    static T const value=Head;
};

template<typename T, T Head, T... Tail>struct maxc<T,Head,Tail...>
{
 private:
    static T const tail=maxc<T,Tail...>::value;
 public:
    static T const value=Head>tail?Head:tail;
};
  
  
template<typename T, T... Args> T maxf()
{
    return maxc<T,Args...>::value;
}

int main()
{
   unsigned int a = maxf<unsigned int, 3, 2>();
   unsigned int b = maxf<unsigned int, 3, 2, 5>();
   return a==b;
}

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux