[4.6.2] problem compiling templates

Jonathan Wakely jwakely.gcc@gmail.com
Tue Nov 29 15:30:00 GMT 2011


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



More information about the Gcc-help mailing list