473,394 Members | 1,935 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,394 software developers and data experts.

C++ template

Hello,

The following code works (it compiles ok with "g++ test.c"):

++++++++++++++++++++++++++++++++++++++++
template <class Valueclass Test1 {
public:
int a;
};

class Test2 : public Test1<int{
public:
void f() {
a = 2;
}
};
++++++++++++++++++++++++++++++++++++++++
However, the following code does not compile:
++++++++++++++++++++++++++++++++++++++++
template <class Valueclass Test1 {
public:
int a;
};

template <class Valueclass Test2 : public Test1<Value{
public:
void f() {
a = 2;
}
};
++++++++++++++++++++++++++++++++++++++++
the compiler (g++-4.0.1) says:
test.c: error: a was not declared in this scope
What is the problem?
ps: the code above does not do anything interesting.
It is just here to illustrate the problem that I currently find while compiling
my other real program.
Many thanks,
DAvid

Aug 6 '08 #1
10 1606
Unkown to Xnntp wrote:
Hello,
<...>

template <class Valueclass Test2 : public Test1<Value{
public:
void f() {
Test1<Value>:: a = 2;
}
};

Think its something like the above. Because you inherit from template
you need to be explicit about base . Something to do with
specialisations etc where in specialisaion compiler cant assume same as
default.

Untested in gcc ( orig works OK in VC8)

regards
Andy Little
Aug 6 '08 #2
Unkown to Xnntp <unkown@xnntpwrites:
However, the following code does not compile:
++++++++++++++++++++++++++++++++++++++++
template <class Valueclass Test1 {
public:
int a;
};
template <class Valueclass Test2 : public Test1<Value{
public:
void f() {
a = 2;
}
};
++++++++++++++++++++++++++++++++++++++++
the compiler (g++-4.0.1) says:
test.c: error: .a. was not declared in this scope

What is the problem?
With this->a=2; it works.

--
__Pascal Bourguignon__
Aug 6 '08 #3
On Aug 6, 4:06 pm, Unkown to Xnntp <unkown@xnntpwrote:
template <class Valueclass Test1 {
public:
int a;

};

template <class Valueclass Test2 : public Test1<Value{
public:
void f() {
a = 2;
}};
the compiler (g++-4.0.1) says:
test.c: error: a was not declared in this scope
What is the problem
Hi,
The problem is that Test1<Valueis a dependent base class and the
variable a is a non-dependent name. Compilers that use 2 phase lookup
lookup the non-dependent names early but the dependent names can't be
resolved when parsing templates. The are resolved at instantiation (ph
2). Hence the name a is ambiguous.

To resolve this you could make the name "a" also dependent using
either of:

Test1<Value::a = 2;
this->a = 2;

For your case either line will work fine but there are situations when
one might be preferred over the other.

regards,
Aman Angrish

Aug 6 '08 #4
"aman.c++" <am******@gmail.comwrote:
...
Test1<Value::a = 2;
this->a = 2;
Thanks everybody for your answers and explanations!

DAvid
Aug 6 '08 #5
"aman.c++" <am******@gmail.comwrote:
On Aug 6, 4:06 pm, Unkown to Xnntp <unkown@xnntpwrote:
>template <class Valueclass Test1 {
public:
int a;

};

template <class Valueclass Test2 : public Test1<Value{
public:
void f() {
a = 2;
}};
the compiler (g++-4.0.1) says:
test.c: error: a was not declared in this scope
What is the problem
Hi,
The problem is that Test1<Valueis a dependent base class and the
variable a is a non-dependent name. Compilers that use 2 phase lookup
lookup the non-dependent names early but the dependent names can't be
resolved when parsing templates. The are resolved at instantiation (ph
2). Hence the name a is ambiguous.

To resolve this you could make the name "a" also dependent using
either of:

Test1<Value::a = 2;
this->a = 2;

For your case either line will work fine but there are situations when
one might be preferred over the other.

regards,
Aman Angrish

One more question,

is there a difference between these two possibilities in terms of
execution time?
or, once compiled, they look exactly the same?
Best regards,
DAvid
Aug 6 '08 #6
Unkown to Xnntp wrote:
template <class Valueclass Test2 : public Test1<Value{
Hate to nitpick about style, but newlines *are* allowed to be used in
source code for clarity.
Aug 6 '08 #7
On Aug 6, 2:28 pm, "aman.c++" <aman....@gmail.comwrote:
On Aug 6, 4:06 pm, Unkown to Xnntp <unkown@xnntpwrote:
template <class Valueclass Test1 {
public:
int a;
};
template <class Valueclass Test2 : public Test1<Value{
public:
void f() {
a = 2;
}};
the compiler (g++-4.0.1) says:
test.c: error: a was not declared in this scope
What is the problem
The problem is that Test1<Valueis a dependent base class and
the variable a is a non-dependent name.
As written, the compiler treats it as non-dependent. The author
of the code obviously wanted it to be dependent.
Compilers that use 2 phase lookup
In other words, compilers that conform to the C++ standard.
lookup the non-dependent names early but the dependent names
can't be resolved when parsing templates. The are resolved at
instantiation (ph 2). Hence the name a is ambiguous.
Not quite. First, the name isn't ambiguous; it isn't found,
because non-dependent lookup occurs at the point where the
template is defined, not where it is instantiated, and doesn't
take dependent base classes into account. (Note that if he'd
derived from Test1<int>, there'd be no problem.)

Dependent names are more complicated, and are searched both in
the context where the template was defined, and in the context
where it was instantiated, but only using ADL in the latter
case.
To resolve this you could make the name "a" also dependent
using either of:
Test1<Value::a = 2;
this->a = 2;
Yes. It's a shame that one has to resource to such tricks,
however, rather than simply being able to say what one means
(e.g. by explicitly declaring which names should be dependent).
For your case either line will work fine but there are
situations when one might be preferred over the other.
I think that the "this->" is the usual solution, and so it
should be preferred as long as there are no real reasons to do
otherwise.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 6 '08 #8
On Aug 6, 4:18 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Unkown to Xnntp wrote:
template <class Valueclass Test2 : public Test1<Value{
Hate to nitpick about style, but newlines *are* allowed to be
used in source code for clarity.
I agree, and the above would be three or four lines in my own
code. But I think a lot of people compress the code when
posting, on the grounds that a ten line posting is more likely
to get answers than a twenty line one.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 6 '08 #9
In article <F1****************************************@beta06 .com>,
Unkown to Xnntp <unkown@xnntpwrote:
>... the following code does not compile:
template <class Valueclass Test1 {
public:
int a;
};

template <class Valueclass Test2 : public Test1<Value{
public:
void f() {
a = 2;
}
};
the compiler (g++-4.0.1) says:
test.c: error: a was not declared in this scope

What is the problem?
ps: the code above does not do anything interesting.
It is just here to illustrate the problem that I currently find
while compiling my other real program.
http://www.comeaucomputing.com/techt...membernotfound
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 6 '08 #10
In article <71*********************************************** ****@gmail.com>,
David Portabella <da**************@gmail.comwrote:
>"aman.c++" <am******@gmail.comwrote:
>On Aug 6, 4:06 pm, Unkown to Xnntp <unkown@xnntpwrote:
>>template <class Valueclass Test1 {
public:
int a;

};

template <class Valueclass Test2 : public Test1<Value{
public:
void f() {
a = 2;
}};
the compiler (g++-4.0.1) says:
test.c: error: a was not declared in this scope
What is the problem
Hi,
The problem is that Test1<Valueis a dependent base class and the
variable a is a non-dependent name. Compilers that use 2 phase lookup
lookup the non-dependent names early but the dependent names can't be
resolved when parsing templates. The are resolved at instantiation (ph
2). Hence the name a is ambiguous.

To resolve this you could make the name "a" also dependent using
either of:

Test1<Value::a = 2;
this->a = 2;

For your case either line will work fine but there are situations when
one might be preferred over the other.

is there a difference between these two possibilities in terms of
execution time?
or, once compiled, they look exactly the same?
Should be the same. If you will, consider a composite of both
as to what is happening in either case.
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 6 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Alfonso Morra | last post by:
I have a class declared as ff: class __declspec(dllexport) A { public: A() ; A(const A&) A& operator=(const A&) ; ~A() ; void doThis(void) ;
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
2
by: Gary Nastrasio | last post by:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and I'm a bit confused by one bit of template syntax in chapter 1. Here is a code example: template <class CreationPolicy>...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.