This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: [4.6.2] problem compiling templates
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Bogdan Slusarczyk <Bogdan dot Slusarczyk at aldec dot com dot pl>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Tue, 29 Nov 2011 14:00:08 +0000
- Subject: Re: [4.6.2] problem compiling templates
- References: <4ED4BE50.6020202@aldec.com.pl>
2011/11/29 Bogdan Slusarczyk:
> Hello everybody,
>
> I noticed strange problem using gcc4.6.2, see please attached file.
>
> Compiling this file I get:
>
> In member function 'void Test<_enum>::foo()':
> error: declaration of 'Predicate _enum'
> error: ?shadows template parm 'Enum _enum'
>
> However it's enough to create intermediate object to avoid this problem (it
> also compiles ok with gcc3.4.3).
>
> Am I doing wrong something here or this is known problem?
You have met C++'s most vexing parse:
http://en.wikipedia.org/wiki/Most_vexing_parse
This line does not do what you think:
Filter< Predicate > filter( Predicate( _enum ) ); //error is here
This declares a function called filter which returns a
Filter<Predicate> and has a single argument called _enum of type
Predicate.
Because your Filter type's constructor is not explicit you can write it as:
Filter< Predicate > filter = Predicate( _enum );
Or you could write:
Filter< Predicate > filter = Filter<Predicate>(Predicate( _enum ));
Or you can use an intermediate object, as you have discovered.
Or in C++11 you can do:
Filter< Predicate > filter{ Predicate{ _enum } };