Re: just C++ in xcode
Re: just C++ in xcode
- Subject: Re: just C++ in xcode
- From: Andreas Grosam <email@hidden>
- Date: Thu, 29 Sep 2005 21:24:18 +0200
On 29.09.2005, at 20:19, Markian Hlynka wrote:
Ah! I get it! You mean because
x=1;
wasn't actually in a code block! Thus, it's an illegal definition. Sorry, missed that!
But why, in your second example below (of 3) is x = 10; assigning to n::x and not the x in global namespace?
secondly, in place of "int x = 1;", could I use "n::x = 1", ie just before the function, or is that illegal?
I'm sorry, the 2. example is not valid. The "using namespace n" directive introduces an ambiguity to name x, so that this example is wrong and will not compile. The corrected version is
#include <iostream>
namespace n { int x; };
using namespace std;
using namespace n;
int x = 1; // declaration/definition of x in global space
static void func()
{
n::x = 10; // requires qualifier n, due to ambiguity with ::x
cout << ::x << endl; // prints ::x in global space: "1"
cout << n::x << endl; // prints n::x "10"
}
Although there is an ambiguity in x, which requires to qualify x, the names cout and endl work nicely without qualifiers.
Without ambiguity of name x:
#include <iostream>
namespace n { int x; };
using namespace std;
using namespace n;
static void func()
{
x = 10; // modfies n::x
cout << x << endl; // prints n::x "10"
}
Hope, that clarifies. More details in the "name lookup rules" stated in the standard.
Again, I appologize for the invalid example. :-(
The 3. example using qualified names is ok, though.
Andreas
Markian
This is just illegal code (as explained previously):
#include <header.h>
using namespace n;
x = 1; // error: illegal definition without a type.
// This is not a reference to n::x!
// A relaxed compiler would probably introduce x of type int
// in global space as if it were written:
// int x = 1;
Here it works as you expect:
bummer: this one is not valid!!
#include <iostream>
#include <n.h>
using namespace std;
using namespace n;
int x = 1; // declaration-definition of x in global space
static void func()
{
x = 10; // that's an assignment to n::x
cout << ::x << endl; // prints ::x "1"
cout << x << endl; // prints n::x "10"
}
this one is ok
without "using":
#include <iostream>
#include <n.h>
int x = 1;
void func();
static void func()
{
n::x = 10;
std::cout << ::x << std::endl; // prints ::x "1"
std::cout << n::x << std::endl; // prints n::x "10"
}
Andreas
Chris
On Thu, 29 Sep 2005 15:07:48 +0200, Andreas Grosam wrote
> On 29.09.2005, at 05:53, Marshall Clow wrote:
>
>
> Maybe this is a C++ question not having to do with
> xcode, but I'm desparate...
>
> If I have a named namespace in foo1.h:
...
> namespace n {
> int x;
> };
> using namespace n;
> x = 1;
>
> The statement above
> x = 1;
> actually introduces a new variable in global space which does not have a type.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
----
Quantum mechanics -- the dreams that stuff is made of.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden