471,612 Members | 2,506 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,612 software developers and data experts.

template parameters are hidden by class members


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.

Jul 22 '05 #1
3 1886
Gianni Mariani wrote:

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
Jul 22 '05 #2
Michael Mellor wrote:
Gianni Mariani wrote:

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"?


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
Jul 22 '05 #3
Gianni Mariani wrote:
Michael Mellor wrote:
Gianni Mariani wrote:

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 I meant 14.6.1.3. 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).


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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Agent Mulder | last post: by
3 posts views Thread by martin.druon | last post: by
2 posts views Thread by Amadeus W. M. | last post: by
3 posts views Thread by Michal Nazarewicz | last post: by
2 posts views Thread by vectorizor | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.