* Help with Code Validation of Variadic templates
@ 2007-12-07 21:10 Rene Buergel
2009-08-17 3:18 ` Larry Evans
0 siblings, 1 reply; 3+ messages in thread
From: Rene Buergel @ 2007-12-07 21:10 UTC (permalink / raw)
To: gcc-help
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'
I think, that this code is valid. If not, please point it out to me.
#2:
=====
template<template<typename... Params> class Comp, typename... Params>
void f( Params... p)
{
static_assert( Comp<Params>::value, "" );
}
template <typename T>
class Foo
{
static const int value=1;
};
int main()
{
f<Foo, int, int>( );
}
=====
Here I'm not that sure about the validity of the code, my intention was
to use tr1/type_traits in macros on ordinary variables instead of types.
So, the variadic template f would resolve the type for me. That intended
use was something like f<std::tr1::is_same>( _var1, _var2 );, which
would compile-time-check, if _var1 and _var2 are exaclty the same type.
But i'm not even coming that far, as gcc issues: "error: no matching
function for call to 'f()'". Any Ideas, what's wrong here?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Help with Code Validation of Variadic templates
2007-12-07 21:10 Help with Code Validation of Variadic templates Rene Buergel
@ 2009-08-17 3:18 ` Larry Evans
2009-08-17 5:08 ` Larry Evans
0 siblings, 1 reply; 3+ messages in thread
From: Larry Evans @ 2009-08-17 3:18 UTC (permalink / raw)
To: gcc-help
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:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40092
A 1 line patch to gcc/cp/tree.c has been proposed there.
You might try the patch an see if it solves your problem.
-regards,
Larry
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Help with Code Validation of Variadic templates
2009-08-17 3:18 ` Larry Evans
@ 2009-08-17 5:08 ` Larry Evans
0 siblings, 0 replies; 3+ messages in thread
From: Larry Evans @ 2009-08-17 5:08 UTC (permalink / raw)
To: gcc-help
[-- Attachment #1: Type: text/plain, Size: 1149 bytes --]
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
[-- Attachment #2: maxv.cpp --]
[-- Type: text/x-c++src, Size: 533 bytes --]
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;
}
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2009-08-16 15:15 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-07 21:10 Help with Code Validation of Variadic templates Rene Buergel
2009-08-17 3:18 ` Larry Evans
2009-08-17 5:08 ` Larry Evans
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).