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

Code question that involves namespaces, templates and classes

Hi!

I just tried to write a little program, but GCC (Mingw 3.3.1) refused to
compile it. So I'd like to know if there's something wrong with my code
or if it is a bug in GCC.

---snip---
namespace Namespace {}

template <class TSomeType>
class TAlpha
{
public:
int Namespace;
};

template <class TSomeType>
class TBeta : public TAlpha<TSomeType>
{
public:
int method(int n) {Namespace=n;}
};

int main()
{
}
---snip---

Gcc complains about using a namespace "Namespace" as expression, while
it should in fact access TAlpha::Namespace. So, is it a bug in GCC or is
my code simply wrong?

TIA
Bernd Fuhrmann
Jul 22 '05 #1
8 1406
"Bernd Fuhrmann" <si**********@gmx.de> wrote...
I just tried to write a little program, but GCC (Mingw 3.3.1) refused to
compile it. So I'd like to know if there's something wrong with my code
or if it is a bug in GCC.

---snip---
namespace Namespace {}

template <class TSomeType>
class TAlpha
{
public:
int Namespace;
};

template <class TSomeType>
class TBeta : public TAlpha<TSomeType>
{
public:
int method(int n) {Namespace=n;}
Your 'int' function is also supposed to return something.
};

int main()
{
}
---snip---

Gcc complains about using a namespace "Namespace" as expression, while
it should in fact access TAlpha::Namespace.
Should it? Why do you say that? And why don't you help it by fully
qualifying the name you need?
So, is it a bug in GCC or is
my code simply wrong?


Your code is simply wrong (as are your assumptions). Every name is
looked up during the syntax check of the template (you don't have it
instantiated, so no other check is done). In the expression in
question, there is a name of a namespace on the left of the assignment
operator.

Victor
Jul 22 '05 #2
Victor Bazarov wrote:
Your 'int' function is also supposed to return something. ACK.
Gcc complains about using a namespace "Namespace" as expression, while
it should in fact access TAlpha::Namespace.


Should it? Why do you say that?


Because it doesn't seem wise to check names in a not (yet) used piece of
code, while instantiation of it might change the accessed C++
constructs. Furthermore I think templates should not interfere in that
way. Ok, I admit it's just a feeling of what C++ should be like, but I'm
not yet totally convinced that my little piece of code is definately wrong.
And why don't you help it by fully
qualifying the name you need?


Well I might, of course. But I didn't and I was wondering why it did not
work and if that is justified.
So, is it a bug in GCC or is
my code simply wrong?

Your code is simply wrong (as are your assumptions). Every name is
looked up during the syntax check of the template (you don't have it
instantiated, so no other check is done). In the expression in
question, there is a name of a namespace on the left of the assignment
operator.


Obviously, yes. But how can the compiler know for sure that this name
does refer to a namespace while instantiation might change that?

Is that kind of name checking founded in ISO C++ or is it just the usual
way to do things in a C++ compiler?

TIA again,
Bernd Fuhrmann
Jul 22 '05 #3
"Bernd Fuhrmann" <si**********@gmx.de> wrote...
Victor Bazarov wrote:
Your 'int' function is also supposed to return something. ACK.
Gcc complains about using a namespace "Namespace" as expression, while
it should in fact access TAlpha::Namespace.


Should it? Why do you say that?


Because it doesn't seem wise to check names in a not (yet) used piece of
code, while instantiation of it might change the accessed C++
constructs. Furthermore I think templates should not interfere in that
way. Ok, I admit it's just a feeling of what C++ should be like, but I'm
not yet totally convinced that my little piece of code is definately

wrong.
> And why don't you help it by fully
qualifying the name you need?
Well I might, of course. But I didn't and I was wondering why it did not
work and if that is justified.


OK
So, is it a bug in GCC or is
my code simply wrong?

Your code is simply wrong (as are your assumptions). Every name is
looked up during the syntax check of the template (you don't have it
instantiated, so no other check is done). In the expression in
question, there is a name of a namespace on the left of the assignment
operator.


Obviously, yes. But how can the compiler know for sure that this name
does refer to a namespace while instantiation might change that?


Because the namespace is the first name found (since it's the first
name available in all scopes searched). Instantiation will NOT change
that.
Is that kind of name checking founded in ISO C++ or is it just the usual
way to do things in a C++ compiler?


Of course it is founded in ISO C++. Why do you think compiler creators
do it that way, out of spite? Out of malice? See 3.4.1 for more info.

Victor
Jul 22 '05 #4
Victor Bazarov wrote:
Because the namespace is the first name found (since it's the first
name available in all scopes searched). Instantiation will NOT change
that.
Obvious. But my point is: Is it a good idea to do have name checking (or
whatever you would like to call the step of looking up "Namespace" and
finding a namespace) when there are not yet all scopes available since
there?

You could also see it the other way round: Is it wise to use a
fundamently different name-capsule-priority in templates than in normal
classes (where inner elements override any outer elements in case of
naming conflicts). It doesn't seem to me.

Btw: When you change
class TBeta : public TAlpha<TSomeType>
to
class TBeta : public TAlpha<int>
it compiles with no complains. So where is the logic in having a look
into TAlpha's name scope only sometimes (if it is not template class
that depends on TBeta's template parameter)?

Maybe it is written in the standart after all that way. If it truly is,
that certainly might be a point that should (at least in my opinion)
improved.
Is that kind of name checking founded in ISO C++ or is it just the usual
way to do things in a C++ compiler?


Of course it is founded in ISO C++. Why do you think compiler creators
do it that way, out of spite? Out of malice?


Even programmers that create compilers are capabable of making mistakes.
Some of these bugs are hard to find. There are (or at least they were
there when I last checked them) some bugs that only appear if you use
almost all cool features of C++, like namespaces, inheritance, and so on
in almost all C++ compilers I tried (MSVC, GCC, Borland Builder). So
some behaviours are implemented in compilers though they are not truly
C++ compliant. That is the reason, why I was asking. See 3.4.1 for more info.

3.4.1 of what (had a look at faq lite, gcc releases, some other faqs)?
Jul 22 '05 #5
"Bernd Fuhrmann" <si**********@gmx.de> wrote...
Victor Bazarov wrote:
Because the namespace is the first name found (since it's the first
name available in all scopes searched). Instantiation will NOT change
that.
Obvious. But my point is: Is it a good idea to do have name checking (or
whatever you would like to call the step of looking up "Namespace" and
finding a namespace) when there are not yet all scopes available since
there?


Yes, it is. Name lookup is already pretty complicated. Introducing
another degree of complexity would just make it worse.
You could also see it the other way round: Is it wise to use a
fundamently different name-capsule-priority in templates than in normal
classes (where inner elements override any outer elements in case of
naming conflicts). It doesn't seem to me.

Btw: When you change
class TBeta : public TAlpha<TSomeType>
to
class TBeta : public TAlpha<int>
it compiles with no complains. So where is the logic in having a look
into TAlpha's name scope only sometimes (if it is not template class
that depends on TBeta's template parameter)?
Since 'TSomeType' is unknown, the TAlpha<TSomeType> can be a different
template specialisation than the original one. Let me illustrate.
Imagine that I have

template<class A> struct AA { int a; };
template<class B> struct BB : AA<B> { int foo() { return a; } };
template<> struct AA<int> { int b; };

Now, what is 'a' in BB? It is _impossible_ to tell because it depends
on what the template argument is. If 'B' is 'int', there _is_no_ 'a'.
Maybe it is written in the standart after all that way. If it truly is,
that certainly might be a point that should (at least in my opinion)
improved.


Then you should go argue about it in 'comp.std.c++'. It makes no sense
doing it here, while there is a whole different place _devoted_ to the
discussions like that.
> See 3.4.1 for more info.

3.4.1 of what (had a look at faq lite, gcc releases, some other faqs)?


The Standard. Get yourself a copy and study it before even considering
making suggestions about it. Just a friendly advice.

Victor
Jul 22 '05 #6
Victor Bazarov wrote:
Then you should go argue about it in 'comp.std.c++'. It makes no sense
doing it here, while there is a whole different place _devoted_ to the
discussions like that.


Ok, you are right. I'll make a posting there.
> See 3.4.1 for more info.

3.4.1 of what (had a look at faq lite, gcc releases, some other faqs)?


The Standard. Get yourself a copy and study it before even considering
making suggestions about it. Just a friendly advice.

Where can I get it (without paying to much money of course)? I always
thought that it costs a lot of money (1000$+) to buy it from ISO.

I finally think that I should have believed you in first place. Thanks
for your patience.

Bernd Fuhrmann
Jul 22 '05 #7
Hi,

Bernd Fuhrmann <si**********@gmx.de> wrote in message news:<bs*************@news.t-online.com>...
Victor Bazarov wrote:
(...)
The Standard. Get yourself a copy and study it before even considering
making suggestions about it. Just a friendly advice.

Where can I get it (without paying to much money of course)? I always
thought that it costs a lot of money (1000$+) to buy it from ISO.


You can get a cheaper PDF copy from ANSI. See:

http://www.parashift.com/c++-faq-lit....html#faq-6.12

--
Wagner
Jul 22 '05 #8
Bernd Fuhrmann wrote in news:bs*************@news.t-online.com:
The Standard. Get yourself a copy and study it before even considering
making suggestions about it. Just a friendly advice.

Where can I get it (without paying to much money of course)? I always
thought that it costs a lot of money (1000$+) to buy it from ISO.

I finally think that I should have believed you in first place. Thanks
for your patience.


Amazon (.co.uk) have a paper version £24.47

http://tinyurl.com/3dse6

You can get the previuos version for $18 in pdf form from ANSI,
maybe in the future you will be able to get the current version
from them at a resonable price too (last time I checked it was
$200+).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #9

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

Similar topics

3
by: Aleksandr Dubinskiy | last post by:
Hello folks, This question is a bit involved, so I am posting the code on the web, instead of cutting and pasting it on the email. http://www.cis.ohio-state.edu/~dubinski/Code/ I think the code...
1
by: Anthony | last post by:
Hello, I have been reading up on unnamed namespaces in the context of hiding classes as mentionned by the GOF Facade pattern description. I was hoping someone could shed some light on this. I...
15
by: Rob Ratcliff | last post by:
I'm compiling the latest version of a CORBA ORB called MICO on a Cray X1. It makes heavy use of templates and namespaces. Up until the link step, the C++ source code compiled flawlessly. But, when...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
25
by: Alvin Bruney | last post by:
C# is great but it does have some short comings. Here, I examine one of them which I definitely think is a shortcoming. Coming from C++, there seems to be no equivalent in C# to separate code...
3
by: Robert W. | last post by:
I'm embarking on a project that will have both a desktop application and a Pocket PC application. It seems logical to have as much code as possible sitting in a shared project, which would be...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.