Template construction in old gcc 3.3.3 does not compile in gcc 3.4.4

E

eknecronzontas

Hello!

Below is a snippet that compiles in 3.3.3, but not in 3.4.4.
Obviously, this is a result of the (laudable) efforts to make gcc more
standards-compliant. Any way to get around this? gcc 3.4.4 complains
that x is undeclared in the template definition.

Thanks,
Andrew Steiner

-----------------------------------------------------------------------

#include <iostream>

using namespace std;

class parent {
public:
parent() { x=3.0; };
double x;
};

template<class parent_t> class child : public parent_t {
public:
void test() {
cout << x << endl;
}
};

int main(void) {
child<parent> c;
c.test();

return 0;
}
 
T

Thomas Tutone

eknecronzontas said:
Below is a snippet that compiles in 3.3.3, but not in 3.4.4.
Obviously, this is a result of the (laudable) efforts to make gcc more
standards-compliant. Any way to get around this? gcc 3.4.4 complains
that x is undeclared in the template definition.

#include <iostream>

using namespace std;

class parent {
public:
parent() { x=3.0; };
double x;
};

template<class parent_t> class child : public parent_t {
public:
void test() {
cout << x << endl;

}
};

int main(void) {
child<parent> c;
c.test();

return 0;
}

Best regards,

Tom
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

Thomas Tutone said:
Change the above line to: cout << this->x << endl;
and all should work as expected.

parent_t::x works too...

Ali
 
E

eknecronzontas

Hehe...should have thought of that. It seems to me that
parent_t::x should be better, as it doesn't require the
dereferencing of the pointer....

Thanks,
Andrew
 
T

Thomas Tutone

Hehe...should have thought of that. It seems to me that
parent_t::x should be better, as it doesn't require the
dereferencing of the pointer....

Actually, the two are functionally identical and should result in
identical compiled code. In both cases you're dereferencing the
pointer - the first time explicitly, the second time implicitly, but it
happens either way.

Best regards,

Tom
 
N

Nathan Addy

I ported some gcc 3.3 code over to gcc 4.0 for the first time last week
and had this exact same problem. Copying and pasting the relevant bits
from the gcc 3.4 release page at
http://gcc.gnu.org/gcc-3.4/changes.html:
======================================================
In a template definition, unqualified names will no longer find members
of a dependent base (as specified by [temp.dep]/3 in the C++ standard).
For example,

template <typename T> struct B {
int m;
int n;
int f ();
int g ();
};
int n;
int g ();
template <typename T> struct C : B<T> {
void h ()
{
m = 0; // error
f (); // error
n = 0; // ::n is modified
g (); // ::g is called
}
};

You must make the names dependent, e.g. by prefixing them with this->.
Here is the corrected definition of C<T>::h,

template <typename T> void C<T>::h ()
{
this->m = 0;
this->f ();
this->n = 0
this->g ();
}
==============================================

Best,
Nathan
 

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
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top