473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1983
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon G Best | last post by:
Hello! I have a query regarding explicit specialisation of class templates which are themselves members of class templates. Here's what I want to do: template< class T > struct pink { ...
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
3
by: martin.druon | last post by:
Hi, I created a template class to represent hypermatrix. I would like to add methods where the number of parameters are checked during the compilation time. For example : template <size_t...
2
by: Amadeus W. M. | last post by:
Template member functions cannot be virtual - I know - but is there any way to simulate that (other than making the entire class templated, with non-templated members). What I have is this: ...
3
by: Michal Nazarewicz | last post by:
Let say I have a class template Pair representing an ordered pair, ie: #v+ template<class T1, class T2> class Pair { T1 a; T2 b; public: Pair() : a(), b() { } Pair(const Pair<T1, T2&p) :...
7
by: Amu | last post by:
Hi i am stuck up in a part of project where i have to inherit the VC++ template classes into C#.net. Please provide me the solution for this .
2
by: vectorizor | last post by:
Hello all, I am attempting to vectorize few template functions with the Intel compiler, but without much success so far. Ok granted, this question is not 100% c++, but it is related enough that...
10
by: Pavel Shved | last post by:
Is there a program of wide use that automatically extracts template parameters requirements from source code of template functions? I mean the following: assume we have a template function that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.