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 

Code question that involves namespaces, templates and classe

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





PostPosted: Sat Dec 27, 2003 6:02 pm    Post subject: Code question that involves namespaces, templates and classe Reply with quote



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
Back to top
Victor Bazarov
Guest





PostPosted: Sat Dec 27, 2003 6:39 pm    Post subject: Re: Code question that involves namespaces, templates and cl Reply with quote



"Bernd Fuhrmann" <silverbanana (AT) gmx (DOT) de> wrote...
Quote:
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 TAlpha
{
public:
int Namespace;
};

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

Your 'int' function is also supposed to return something.

Quote:
};

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?

Quote:
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



Back to top
Bernd Fuhrmann
Guest





PostPosted: Sat Dec 27, 2003 7:02 pm    Post subject: Re: Code question that involves namespaces, templates and cl Reply with quote



Victor Bazarov wrote:
Quote:
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.

Quote:
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.

Quote:
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

Back to top
Victor Bazarov
Guest





PostPosted: Sat Dec 27, 2003 9:23 pm    Post subject: Re: Code question that involves namespaces, templates and cl Reply with quote

"Bernd Fuhrmann" <silverbanana (AT) gmx (DOT) de> wrote...
Quote:
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

Quote:
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.

Quote:
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



Back to top
Bernd Fuhrmann
Guest





PostPosted: Sat Dec 27, 2003 9:54 pm    Post subject: Re: Code question that involves namespaces, templates and cl Reply with quote

Victor Bazarov wrote:
Quote:
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.

Quote:
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.
Quote:
See 3.4.1 for more info.
3.4.1 of what (had a look at faq lite, gcc releases, some other faqs)?


Back to top
Victor Bazarov
Guest





PostPosted: Sat Dec 27, 2003 10:31 pm    Post subject: Re: Code question that involves namespaces, templates and cl Reply with quote

"Bernd Fuhrmann" <silverbanana (AT) gmx (DOT) de> wrote...
Quote:
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.

Quote:
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 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 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'.

Quote:
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.

Quote:
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



Back to top
Bernd Fuhrmann
Guest





PostPosted: Sun Dec 28, 2003 8:07 am    Post subject: Re: Code question that involves namespaces, templates and cl Reply with quote

Victor Bazarov wrote:
Quote:
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.

Quote:
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

Back to top
Wagner Bruna
Guest





PostPosted: Sun Dec 28, 2003 3:10 pm    Post subject: Re: Code question that involves namespaces, templates and cl Reply with quote

Hi,

Bernd Fuhrmann <silverbanana (AT) gmx (DOT) de> wrote

Quote:
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-lite/big-picture.html#faq-6.12

--
Wagner

Back to top
Rob Williscroft
Guest





PostPosted: Sun Dec 28, 2003 3:33 pm    Post subject: Re: Code question that involves namespaces, templates and cl Reply with quote

Bernd Fuhrmann wrote in news:bsm30o$52v$04$1 (AT) news (DOT) t-online.com:

Quote:
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/

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.