473,241 Members | 1,616 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,241 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 1974
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.