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

Name Resolution for Class Derived from Template Argument

Take this code as a base case, it compiles successfully:

struct R
{
int i;
};
class S : public R
{
void F(void) {i = 0;}
};
S s;

Switch to deriving S from R as a template argument and the behavior
varies by compiler:

struct R
{
int i;
};
template <class T>
class S : public T
{
void F(void) {i = 0;}
};
S<Rs;

Visual Studio succeeds building this. GCC fails with "error: 'i'
was not declared in this scope". ICC (Intel's compiler) succeeds with
that instantiation but fails if you attempt an explicit template
instantiation, like:
template class S<R>;

GCC and ICC succeed if you switch to a qualified reference to i:
void F(void) {T::i = 0;}

Is the way namespaces are inherited from base classes supposed to work
differently if those base classes are template arguments or is
something wrong with the compilers?

Nov 27 '06 #1
2 1764

mo*********@yahoo.com napsal:
Take this code as a base case, it compiles successfully:

struct R
{
int i;
};
class S : public R
{
void F(void) {i = 0;}
};
S s;

Switch to deriving S from R as a template argument and the behavior
varies by compiler:

struct R
{
int i;
};
template <class T>
class S : public T
{
void F(void) {i = 0;}
};
S<Rs;

Visual Studio succeeds building this. GCC fails with "error: 'i'
was not declared in this scope". ICC (Intel's compiler) succeeds with
that instantiation but fails if you attempt an explicit template
instantiation, like:
template class S<R>;

GCC and ICC succeed if you switch to a qualified reference to i:
void F(void) {T::i = 0;}

Is the way namespaces are inherited from base classes supposed to work
differently if those base classes are template arguments or is
something wrong with the compilers?
I think that behaviour of gcc is correct. When class is derived from
class defined by template parameter, you cannot access parent's members
with direct name, you have to write either T::member or this->member.

Nov 27 '06 #2
mo*********@yahoo.com wrote:
Switch to deriving S from R as a template argument and the behavior
varies by compiler:

struct R
{
int i;
};
template <class T>
class S : public T
{
void F(void) {i = 0;}
};
S<Rs;

Visual Studio succeeds building this. GCC fails with "error: 'i'
was not declared in this scope". ICC (Intel's compiler) succeeds with
that instantiation but fails if you attempt an explicit template
instantiation, like:
template class S<R>;

GCC and ICC succeed if you switch to a qualified reference to i:
void F(void) {T::i = 0;}

Is the way namespaces are inherited from base classes supposed to work
differently if those base classes are template arguments or is
something wrong with the compilers?
See the FAQ (and the one following it too):
http://www.parashift.com/c++-faq-lit...html#faq-35.19

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Nov 27 '06 #3

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

Similar topics

1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
5
by: Hartmut Sbosny | last post by:
Hello NG, I have a ordinary class `Base' with two functions `void foo()' and `void any()': class Base { public: Base() {} void foo() {} void any() {}
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
13
by: Vladimir Granitsky | last post by:
Hi guys, Please, look at the code below and try to step into it. The compiled code calls the loosely typed method public void Method1(object o) !?!? Am I right that C# compiler does wrong...
5
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After...
5
by: Marcin Gil | last post by:
Hi! I have the code like this (obvious things like ctor/dtor removed) typedef struct _NODE { int val; int index; } Node;
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
9
by: Anthony Williams | last post by:
Hi, Should the following compile, and what should it print? #include <memory> #include <iostream> void foo(std::auto_ptr<intx) { std::cout<<"copy"<<std::endl;
2
by: amkg | last post by:
Hello world, I'm trying to figure out some way to constrain a template class argument for a member function definition, such that the template class argument must, totally, absolutely be a...
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...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.