 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
kunal Guest
|
Posted: Wed Aug 17, 2005 12:41 pm Post subject: where is the difference |
|
|
1.
#include <iostream>
using namespace std ;
namespace PSPL
{
int y = 0 ;
}
namespace P = PSPL ;
namespace
{
int y = 3 ;
}
void Fn()
{
cout << y ;
}
int main()
{
using namespace P ;
Fn() ;
return 0 ;
}
Output of this program is 3.
2.
#include
using namespace std ;
namespace PSPL
{
int y = 0 ;
}
namespace
{
int y = 3 ;
}
int main()
{
using namespace PSPL ;
y = 3 ;
cout << y ;
return 0 ;
}
Output of this Program is compile time error.
what/where is the Differnece between these two code fragments plz
anybody help me to explain the differece.
thx in advance
regards--
kunal.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kwijibo28 Guest
|
Posted: Wed Aug 17, 2005 2:33 pm Post subject: Re: where is the difference |
|
|
In your first example, inside the function Fn() there is no ambiguity
about which y variable you are refering to. It`s the one from the
unnamed namespace. The "using namespace P" you place inside main() just
before calling Fn() as no effect inside the function Fn(). The "using"
directive only apply for the current scope, not inside functions called
in this scope. In fact, in the first example the using directive
completely useless.
On the other hand, in your second example, the using directive has the
effect to put PSPL::y in the global namespace for the scope of the main
function. You are now able to access PSPL::y by writting ::y (or simply
y). The problem here is that you have already define another ::y
variable in the unnamed namespace. So when you reference to ::y inside
main() the compiler will tell you that ::y is ambigous. It does not
know which to choose.
kwijibo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Aug 17, 2005 2:35 pm Post subject: Re: where is the difference |
|
|
kunal wrote:
| Quote: | 1.
#include <iostream
using namespace std ;
namespace PSPL
{
int y = 0 ;
}
namespace P = PSPL ;
namespace
{
int y = 3 ;
}
void Fn()
{
cout << y ;
|
PSPL::y is invisible here. Only
That's why calling 'Fn' causes printing out '3'. The using directive
below has no effect on the body of the 'Fn' function.
| Quote: | }
int main()
{
using namespace P ;
Fn() ;
return 0 ;
}
Output of this program is 3.
2.
#include
using namespace std ;
namespace PSPL
{
int y = 0 ;
}
namespace
{
int y = 3 ;
}
int main()
{
using namespace PSPL ;
y = 3 ;
|
The compile can't know which 'y' you mean here. Both are available.
| Quote: | cout << y ;
return 0 ;
}
Output of this Program is compile time error.
what/where is the Differnece between these two code fragments plz
anybody help me to explain the differece.
|
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
vindhya Guest
|
Posted: Wed Aug 17, 2005 2:37 pm Post subject: Re: where is the difference |
|
|
kunal wrote:
| Quote: | 1.
#include
using namespace std ;
namespace PSPL
{
int y = 0 ;
}
namespace P = PSPL ;
namespace
{
int y = 3 ;
}
void Fn()
{
cout << y ;
}
int main()
{
using namespace P ;
Fn() ;
return 0 ;
}
Output of this program is 3.
In this code namespace being used is P. Your Fn is not creating problem |
since its in global namespace.
| Quote: | 2.
#include
using namespace std ;
namespace PSPL
{
int y = 0 ;
}
namespace
{
int y = 3 ;
}
int main()
{
using namespace PSPL ;
y = 3 ;
cout << y ;
return 0 ;
}
Output of this Program is compile time error.
In this case you are trying to use y. Now y is present in two |
namespace. One is PSPL which compiler picked up from "using namespace
PSPL" and the other has no name so compiler uses it as global (in fact
undefined). Hence it gets confused which one to take, is it from global
or PSPL.
Hence it reports ambiguity.
| Quote: | what/where is the Differnece between these two code fragments plz
anybody help me to explain the differece.
thx in advance
regards--
kunal.
Regards, |
MS
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
shakahshakah@gmail.com Guest
|
Posted: Wed Aug 17, 2005 2:38 pm Post subject: Re: where is the difference |
|
|
kunal wrote:
| Quote: | 1.
#include
using namespace std ;
namespace PSPL
{
int y = 0 ;
}
namespace P = PSPL ;
namespace
{
int y = 3 ;
}
void Fn()
{
cout << y ;
}
int main()
{
using namespace P ;
Fn() ;
return 0 ;
}
Output of this program is 3.
2.
#include
using namespace std ;
namespace PSPL
{
int y = 0 ;
}
namespace
{
int y = 3 ;
}
int main()
{
using namespace PSPL ;
y = 3 ;
cout << y ;
return 0 ;
}
Output of this Program is compile time error.
what/where is the Differnece between these two code fragments plz
anybody help me to explain the differece.
|
In your second example both PSPL::y and the y from the "unnamed"
namespace are both in scope, so y is ambiguous. In the first example,
at the point where Fn() is defined only the "unnamed" y is in scope, so
there's no problem.
See the "Unnamed Namespaces" section of
http://www.winterdom.com/dev/cpp/nspaces.html for a simple explanation
and example of this.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Wed Aug 17, 2005 3:14 pm Post subject: Re: where is the difference |
|
|
kunal wrote:
| Quote: | anybody help me to explain the differece.
|
You do not understand that 'using namespace N'
is a static directive that affects which names
are visible in the scope. It is not a dynamic
statement which changes which names are visible
elsewhere. Your misunderstanding is obvious
given that you wrote
using namespace P ;
Fn() ;
and expected it to affect which y Fn sees.
Names in the unnamed namespace are visible, so
making an identical name from another namespace
visible will cause ambiguity, as you saw.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bo Persson Guest
|
Posted: Wed Aug 17, 2005 3:15 pm Post subject: Re: where is the difference |
|
|
"kunal" <kunal.jethwa (AT) gmail (DOT) com> skrev i meddelandet
news:1124269813.696247.152970 (AT) g43g2000cwa (DOT) googlegroups.com...
| Quote: | 1.
#include
using namespace std ;
namespace PSPL
{
int y = 0 ;
}
namespace P = PSPL ;
namespace
{
int y = 3 ;
}
void Fn()
{
cout << y ;
}
|
At this point, only the y in the anonymous namespace is visible. It is
decided at this point that the function Fn() uses that y.
| Quote: | int main()
{
using namespace P ;
|
Here you make another variable y visible, but that does not affect
function Fn, because it was already defined above. That doesn't change
even if other names are introduced later.
| Quote: | Fn() ;
return 0 ;
}
Output of this program is 3.
|
Seems ok to me.
| Quote: |
2.
#include
using namespace std ;
namespace PSPL
{
int y = 0 ;
}
namespace
{
int y = 3 ;
}
|
At this point, the y in the anonymous namespace is visible, just like
in example 1.
| Quote: | int main()
{
using namespace PSPL ;
|
Here you make another y visible. Now there are two of them, and the
compiler cannot choose between them.
| Quote: | y = 3 ;
cout << y ;
return 0 ;
}
Output of this Program is compile time error.
|
Bo Persson
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Simon Bone Guest
|
Posted: Fri Aug 19, 2005 2:33 pm Post subject: Re: where is the difference |
|
|
On Wed, 17 Aug 2005 08:41:54 -0400, kunal wrote:
| Quote: | 1.
#include <iostream
using namespace std ;
|
The above is common boilerplate in examples, but of course,
avoid using it in header files. Including
in header files, without any "using namespace std;". I assume
you know this, but other might not.
You might get some sense of the reasons from your example though!
| Quote: | namespace PSPL
{
int y = 0 ;
|
This is introducing a new object into the program. The object is called
"::PSPL::y" and it is of type int (a built-in type)
| Quote: | }
namespace P = PSPL ;
|
This creates an alias for the PSPL namespace, so that any name begining
::PSPL can be referred to with : (which is shorter and so convienient).
This is the begining of an anonymous namespace. It is given a secret name
by the implementation, one you can't otherwise use. I'll use ::$_1 below,
so that I can refer to it. If the same code appeared in another
translation unit it would get a different name, such as ::$_2 etc.
Now we introduce another new object, called "::$_1::y".
So far no confusion I hope. But by using the long-hand names, you are
probably begining to appreciate the assistance the namespace language
feature offers. Its extra typing to do without, and more visual clutter.
But on the plus side - the corner cases like this one are easier to
follow.
Now we have left the anonymous namespace. We can't refer to it by name, so
there is an implicit statement here:
using namespace ::$_1;
And again, in another translation unit, with the same code the name would
be changed. That stops us from naming the object ::$_1::y except in this
translation unit. There are ways to reach it - such as passing out
pointers or references to it but no way to directly name it.
| Quote: | void Fn()
{
cout << y ;
|
At this point we are referring to ::$_1::y because we have the using
directive above. We could also name : ::y (or as ::PSPL::y ) explicitly.
Remember we can't specify the name ::$_1::y long-hand because one compiler
might use the name I have but another could use something quite different,
as long as it was impossible for the portable C++ programmer to accidently
introduce a collision.
| Quote: | }
int main()
{
using namespace P ;
|
Right here, we could refer to : ::y as plain y, but we don't so the
compiler will not complain.
This is a call to Fn above. The code is not cut-and-pasted in, unlike a
macro. The definition provided above has to be used.
By the way, you don't show it here, but think about what happens if you
put the definition for Fn in a different translation unit from main...
| Quote: | return 0 ;
}
}
}
Output of this program is 3.
|
Quite as it should be. That is a portable behaviour.
| Quote: | 2.
#include
using namespace std ;
namespace PSPL
{
int y = 0 ;
}
namespace
{
int y = 3 ;
}
int main()
{
using namespace PSPL ;
|
The difference in the example here is there is no function call. But most
of the previous discussion would apply.
Now is that ::PSPL::y or ::$_1::y?
The compiler cannot tell...
| Quote: | cout << y ;
return 0 ;
}
}
Output of this Program is compile time error.
|
So we get an error.
| Quote: | what/where is the Differnece between these two code fragments plz
anybody help me to explain the differece.
thx in advance
regards--
kunal.
|
Hope that helps. It would be worth checking the FAQ now, if you haven't
already. Some of the sample code there is probably going to make more
sense to you now.
Simon Bone
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bharat Karia Guest
|
|
| Back to top |
|
 |
kwijibo28 Guest
|
Posted: Mon Aug 22, 2005 2:28 pm Post subject: Re: where is the difference |
|
|
Simon Bone wrote:
| Quote: | On Wed, 17 Aug 2005 08:41:54 -0400, kunal wrote:
1.
#include <iostream
using namespace std ;
The above is common boilerplate in examples, but of course,
avoid using it in header files. Including
in header files, without any "using namespace std;". I assume
you know this, but other might not.
|
Well I've certainly learn something this morning. I've never bother to
look what's inside <iosfwd>. This is really interesting, you don't have
to write yourself the ugly templated forward reference. In fact, I'm
wondering if every standard header should have forward declaration
counterpart (Any thought...).
Sometime when you learn things by yourself you can miss the obvious...
kwijibo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Simon Bone Guest
|
Posted: Tue Aug 23, 2005 6:46 am Post subject: Re: where is the difference |
|
|
On Mon, 22 Aug 2005 10:28:03 -0400, kwijibo28 wrote:
| Quote: |
Simon Bone wrote:
On Wed, 17 Aug 2005 08:41:54 -0400, kunal wrote:
1.
#include <iostream
using namespace std ;
The above is common boilerplate in examples, but of course,
avoid using it in header files. Including
in header files, without any "using namespace std;". I assume
you know this, but other might not.
Well I've certainly learn something this morning. I've never bother to
look what's inside <iosfwd>. This is really interesting, you don't have
to write yourself the ugly templated forward reference. In fact, I'm
wondering if every standard header should have forward declaration
counterpart (Any thought...).
|
You might not even be able to write the ugly templated forward reference
yourself. Some implementators want to be allowed the freedom to add extra
template parameters, though they would ensure they have a default that
lets you use them in the usual way. The only portable way to use the
standard library is by including the standard headers.
And yes, some people would like extra forward declaration headers, since
most implementations clutter the standard headers with the standard
library implementation. A standard <stringfwd> seems to be the most
popular request, since you can only use std::string and std::wstring
portably anyway. However, pre-compiled headers made a big difference. If
you have them (and most implementations do now) you probably won't benefit
much from extra forward declaration headers.
It is worth noting that some experts recommend providing your own code
libraries with ...fwd headers. See the new "Effective C++" (3rd edition)
item 31. Since you are self-studing, I strongly recommend that book.
| Quote: | Sometime when you learn things by yourself you can miss the
obvious...
|
A lot of people have missed this. The existing experience from C didn't
really translate to modern C++ (with templates et al). If you are starting
out learning C++ as a new language, you might well find you are
leap-frogging more experienced programmers in your use of the language.
Simon Bone
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|