template parameters are hidden by class members

G

Gianni Mariani

I was a little surprised by this:

It seems like the code below should not compile but the Comeau 4.3.3
compiler accepts it and the gcc 3.4(prerel) compiler rejects it and
MSVC++7.1 ICE's.

14.6.1 in the standard seems to imply that template parameters are
hidden by class members.

struct X
{
struct C { int x; };
class Z { Z(int){} };

template<typename T,typename A, template<typename,typename> class C>
C<T, A> Func()
{
return C<T, A>(); // should find X::C which is not a template
}

template< typename Z > Z Zunc()
{
return Z(); //this is X::Z and fail on undefined constructor
}

template< typename Z > void Zinc()
{
}

void tst()
{
Zunc<int>();

Zinc<int>();
}
};

Notch that up to another C++ lesson.
 
M

Michael Mellor

Gianni said:
I was a little surprised by this:

It seems like the code below should not compile but the Comeau 4.3.3
compiler accepts it and the gcc 3.4(prerel) compiler rejects it and
MSVC++7.1 ICE's.

14.6.1 in the standard seems to imply that template parameters are
hidden by class members.

struct X
{
struct C { int x; };
class Z { Z(int){} };

template<typename T,typename A, template<typename,typename> class C>
C<T, A> Func()
{
return C<T, A>(); // should find X::C which is not a template
}

template< typename Z > Z Zunc()
{
return Z(); //this is X::Z and fail on undefined constructor
}

template< typename Z > void Zinc()
{
}

void tst()
{
Zunc<int>();

Zinc<int>();
}
};

Notch that up to another C++ lesson.

From the standard:
14.6.3 states "The scope of a templateparameter extends from its point
of declaration until the end of its template. A templateparameter hides
any entity with the same name in the enclosing scope"

which supports the result you are getting form Comeau. Which specific
part of the standard do you think supports your claim that "template
parameters are hidden by class members"?

Michael Mellor
 
G

Gianni Mariani

Michael said:
From the standard:
14.6.3 states "The scope of a templateparameter extends from its point
of declaration until the end of its template. A templateparameter hides
any entity with the same name in the enclosing scope"

which supports the result you are getting form Comeau. Which specific
part of the standard do you think supports your claim that "template
parameters are hidden by class members"?

At the end of 14.6.1 it shows that class members hide template
parameters - so even though the have the scope you describe, they are
"hidden" by class members. (which makes the parameter kind of useless
imho and should probably fire a warning at least).

See the discussion on this pr in the gcc bug system.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13967
 
M

Michael Mellor

Gianni said:
At the end of 14.6.1 it shows that class members hide template
parameters - so even though the have the scope you describe, they are
"hidden" by class members. (which makes the parameter kind of useless
imho and should probably fire a warning at least).

14.6.1.6 states:
"In the definition of a member of a class template that appears outside
of the namespace containing the class template definition, the name of a
templateparameter hides the name of a member of this namespace."

In your example there is not a class template just a member-template.
See the discussion on this pr in the gcc bug system.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13967
The reasoning of that thread is that this is illgeal:
"
struct X {
int C;

template<class> void func();
};


template<class C>
void X::func()
{
C c; // ERROR: C is not a type.
}
"
I cannot find support for it finding X::C over the template-parameter C
in the standard. If the example was:
template<class A>
struct X {
int C;

void func();
};


template<class C>
void X<C>::func()
{
C c; // ERROR: C is not a type.
}
then I agree with the logical.

Michael Mellor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,188
Messages
2,571,004
Members
47,599
Latest member
eidatovo

Latest Threads

Top