C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

is g++ wrong?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
steve yee
Guest





PostPosted: Tue Sep 12, 2006 9:10 am    Post subject: is g++ wrong? Reply with quote



g++ can't compile this code:

#include <iostream>
using namespace std;

template <typename T>
class Outer
{
public:
class Base
{
protected:
int m_n;
};

class Derived : public Base
{
public:
Derived(int n)
{
m_n = n; // g++ says m_n undeclared
}
void print()
{
cout << m_n << endl;
}
};
};

int main(int argc, char *argv[])
{
Outer<int>::Derived(100).print();
return 0;
}
Back to top
Salt_Peter
Guest





PostPosted: Tue Sep 12, 2006 9:10 am    Post subject: Re: is g++ wrong? Reply with quote



steve yee wrote:
Quote:
g++ can't compile this code:

#include <iostream
using namespace std;

template <typename T
class Outer
{
public:
class Base
{
protected:
int m_n;
};

class Derived : public Base
{
public:
Derived(int n)
{
m_n = n; // g++ says m_n undeclared
}
void print()
{
cout << m_n << endl;
}
};
};

int main(int argc, char *argv[])
{
Outer<int>::Derived(100).print();
return 0;
}

You can't ask a compiler to guess what its suppose to do with that
template parameter. At the very least, a templated class will require a
ctor. There is also the fundamental question of what that "Outer" class
is suppose to bring to this design.

Maybe i'm wrong but you are attempting to solve a technical issue when
what i see is lack of a more fundamental startegy.

#include <iostream>

namespace Outer
{
template< typename T >
class Base
{
T m_t;
public:
Base() : m_t(0) { }
Base(T t) : m_t(t) { }
~Base() { }
T get() const { return m_t; }
};

template< typename T >
class Derived : public Base< T >
{
public:
Derived() { }
Derived(T t) : Base< T >(t) { }
~Derived() { }
void display() const { std::cout << Base< T >::get() <<
std::endl; }
};

} // namespace

int main()
{
Outer::Derived< int > d0;
Outer::Derived< int > d1(100);

d0.display();
d1.display();

return 0;
}
Back to top
Jens Theisen
Guest





PostPosted: Tue Sep 12, 2006 9:10 am    Post subject: Re: is g++ wrong? Reply with quote



Hi Steve,

g++ probably is right. Qualifying as m_n does satisfy it. Comeau also
agrees with g++. One could argue that In the context of Outer, Base
isn't dependent on a template (as none of the other members are) and
so m_n should be found unqualified, but that doesn't appear to be how
it's generally implemented.

Does anyone know the relevant passages in the standard?

Jens
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.