473,790 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<TSomeTyp e>
{
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::Namespa ce. So, is it a bug in GCC or is
my code simply wrong?

TIA
Bernd Fuhrmann
Jul 22 '05 #1
8 1430
"Bernd Fuhrmann" <si**********@g mx.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<TSomeTyp e>
{
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::Namespa ce.
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::Namespa ce.


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**********@g mx.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::Namespa ce.


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<TSomeTyp e>
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**********@g mx.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<TSomeTyp e>
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<TSomeTyp e> 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**********@g mx.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
1907
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 is pretty clear, just there's a lot of it, so I'll try to guide you thru it. Here's the layout. There are 5 packages / namespaces: Updater, Energizer, Converter, Proposal, and finally Diffuser
1
1679
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 would like the following classes where Facade has access to Alpha and Beta which are otherwise hidden. class Alpha {
15
425
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 it tried to link, I got the attached warnings and then an error. Any ideas why the linker wouldn't see the objects in the library? They look like pretty long names, so maybe there is some type of symbol length or mangling issue going on? The...
45
3628
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 themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
25
2137
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 cleanly for the user's benefit. Why is this important? Because a user gets to maintain this code, not a machine. C++ exposed us to header files which was a way, among other things, to cleanly separate class implementation from declaration. Why was...
3
2853
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 referenced and utilized by both the Windows Forms application and the Mobile Device application. Are there any "gotchas" (ie. warnings) that anyone knows about in following this approach? Robert W. Vancouver, BC
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10413
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9986
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.