By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,018 Members | 870 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,018 IT Pros & Developers. It's quick & easy.

Code question that involves namespaces, templates and classes

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.