Code question that involves namespaces, templates and classes

B

Bernd Fuhrmann

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
 
V

Victor Bazarov

Bernd Fuhrmann said:
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
 
B

Bernd Fuhrmann

Victor said:
Your 'int' function is also supposed to return something. ACK.

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.
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
 
V

Victor Bazarov

Bernd Fuhrmann said:
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.

Well I might, of course. But I didn't and I was wondering why it did not
work and if that is justified.
OK


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
 
B

Bernd Fuhrmann

Victor said:
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.
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)?
 
V

Victor Bazarov

Bernd Fuhrmann said:
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.
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
 
B

Bernd Fuhrmann

Victor said:
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.
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
 
R

Rob Williscroft

Bernd Fuhrmann wrote in
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.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top