Compilation problem when upgrading from g++ 3.3 to g++ 3.4

A

Alex Vinokur

Hi,

The code below has no problem with GNU g++ 3.3,
but it has a problem with GNU g++ 3.4.

What is reason for that?

--------- [C++] foo.cpp : BEGIN ---------
template <typename T>
struct Boo
{
bool value_;
};

template <typename T>
struct Doo : public Boo<T>
{
Doo () {value_ = false;} // Line#10
};

int main()
{
Doo<int> inst;
return 0;
}
--------- [C++] foo.cpp : END ---`--------



------ Compilation with GNU gcc 3.3 (Cygwin) : BEGIN ------

$ g++ --version
g++ (GCC) 3.3.3 (cygwin special)
[---omitted---]

$ g++ -W -Wall foo.cpp
// No errors/warnings

------ Compilation with GNU gcc 3.3 (Cygwin) : END --------



------ Compilation with GNU gcc 3.4 (DJGPP) : BEGIN ------

$ gpp --version
gpp.exe (GCC) 3.4.1
[---omitted---]


$ gpp -W -Wall foo.cpp
foo.cpp: In constructor `Doo<T>::Doo()':
foo.cpp:10: error: `value_' undeclared (first use this function)
foo.cpp:10: error: (Each undeclared identifier is reported only once for each
function it appears in.)


------ Compilation with GNU gcc 3.4 (DJGPP) : END --------
 
S

Sharad Kala

Alex Vinokur said:
Hi,

The code below has no problem with GNU g++ 3.3,
but it has a problem with GNU g++ 3.4.

I could guess your problem from just the line above :)
What is reason for that?

g++ 3.4 implements the so-called two-phse name lookup. Check this FAQ -
http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.1
7
--------- [C++] foo.cpp : BEGIN ---------
template <typename T>
struct Boo
{
bool value_;
};

template <typename T>
struct Doo : public Boo<T>
{
Doo () {value_ = false;} // Line#10

Doo () {Boo said:

Sharad
 
A

Alex Vinokur

Sharad Kala said:
Alex Vinokur said:
Hi,

The code below has no problem with GNU g++ 3.3,
but it has a problem with GNU g++ 3.4.

I could guess your problem from just the line above :)
What is reason for that?

g++ 3.4 implements the so-called two-phse name lookup. Check this FAQ -
http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.1
7
--------- [C++] foo.cpp : BEGIN ---------
template <typename T>
struct Boo
{
bool value_;
};

template <typename T>
struct Doo : public Boo<T>
{
Doo () {value_ = false;} // Line#10

Doo () {Boo said:

Sharad

Thanks.

Here is new version of the program which contains three ways to resolve this problem
according to http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.17.


template <typename T>
struct Boo
{
bool value1_;
bool value2_;
bool value3_;
};

template <typename T>
struct Doo : public Boo<T>
{
using Boo<T>::value3_;
Doo ()
{
this->value1_ = false;
Boo<T>::value2_ = false;
value3_ = false;
}
};

int main()
{
Doo<int> inst;
return 0;
}
 
J

John Harrison

Or (slightly easier typing)

Doo () {this->value_ = false;} // Line#10

john
 
S

Sharad Kala

John Harrison said:
Or (slightly easier typing)
Doo () {this->value_ = false;} // Line#10

To OP - A special mention here that for virtual functions the above syntax
of this-> should be used, else the call will always resolve to the base
class function.

Sharad
 
M

Maurizio Loreti

Alex Vinokur said:
Doo () {value_ = false;} // Line#10

Change this to : Boo<T>::value_ = false;

See The g++ 3.4.2 Release Notes
(http://gcc.gnu.org/gcc-3.4/changes.html), where they say: "In a
template definition, unqualified names will no longer find members of
a dependent base".

This behaviour is required by the Standard, IIRC in case you have more
specialized definitions of Boo<T>, each one with its particular
implementation, and different meaning/use of value_ .
 
I

Ioannis Vranos

Sharad said:
To OP - A special mention here that for virtual functions the above syntax
of this-> should be used, else the call will always resolve to the base
class function.


He shouldn't call any virtual function at the construction phase.
 
S

Sharad Kala

Ioannis Vranos said:
He shouldn't call any virtual function at the construction phase.

Of course, that's an orthogonal issue that virtuality is not achieved
inside constructors.

Sharad
 
A

Alex Vinokur

Ioannis Vranos said:
Where a public: statement must be added in the base class, otherwise the
code is erroneous.
[snip]

I am using 'struct' (not 'class') in my examples.
 
I

Ioannis Vranos

Alex said:
I am using 'struct' (not 'class') in my examples.

I was talking about the FAQ:


template<typename T>
class B {

Needed here ==> public:

void f() { }
};

template<typename T>
class D : public B<T> {
void g()
{
f();
}
};
 
T

Thomas Maeder

Maurizio Loreti said:
Change this to : Boo<T>::value_ = false;

I prefer

this->value_ = false;

because it's immune to a change of the name of the base class template.
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top