By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

Are numeric constants in a namespace visible globally?

P: n/a
Hello, I am starting to steer away from the practice of using "using
namespace std;" in my code. Instead I am qualifying each name in the source
when I use them, for example: std::cout << "Hello";

Now to my question. Depending upon the status of my program, I return either
EXIT_SUCCESS or EXIT_FAILURE from main(). Thinking that these constants live
in the std namespace, I tried:
return std::EXIT_FAILURE; but my compiler said:
server.cpp:116: error: parse error before numeric constant
So does that mean that all numeric constants in a namespace are visible
globally? Or are EXIT_FAILURE and EXIT_SUCCESS preprocessor macros
(#defines?) and therefore don't care about namespaces? I am including
<cstdlib>. Anything else I should think of when I want to be careful and
qualify each name where it's used? Any caveats?

/ William Payne
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"William Payne" <mi******************@student.liu.se> wrote in message
news:bv**********@news.island.liu.se...
Hello, I am starting to steer away from the practice of using "using
namespace std;" in my code. Instead I am qualifying each name in the source
when I use them, for example: std::cout << "Hello";

Now to my question. Depending upon the status of my program, I return either
EXIT_SUCCESS or EXIT_FAILURE from main(). Thinking that these constants live
in the std namespace, I tried:
return std::EXIT_FAILURE; but my compiler said:
server.cpp:116: error: parse error before numeric constant
So does that mean that all numeric constants in a namespace are visible
globally? Or are EXIT_FAILURE and EXIT_SUCCESS preprocessor macros
(#defines?) and therefore don't care about namespaces? I am including
<cstdlib>. Anything else I should think of when I want to be careful and
qualify each name where it's used? Any caveats?


I think they are preprocessor macros, something like
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

-Sharad
Jul 22 '05 #2

P: n/a
William Payne wrote in news:bv**********@news.island.liu.se:
Hello, I am starting to steer away from the practice of using "using
namespace std;" in my code. Instead I am qualifying each name in the
source when I use them, for example: std::cout << "Hello";

Now to my question. Depending upon the status of my program, I return
either EXIT_SUCCESS or EXIT_FAILURE from main(). Thinking that these
constants live in the std namespace, I tried:
return std::EXIT_FAILURE; but my compiler said:
server.cpp:116: error: parse error before numeric constant
So does that mean that all numeric constants in a namespace are
visible globally? Or are EXIT_FAILURE and EXIT_SUCCESS preprocessor
macros (#defines?) and therefore don't care about namespaces?
Yes, they're macros, the fact that they are all uppercase is a good hint
that that is true. Its only a hint though (std::FILE isn't a macro,
though IIUC in C FILE can be a macro).
I am
including <cstdlib>. Anything else I should think of when I want to be
careful and qualify each name where it's used? Any caveats?


Consider:

#include <iostream>
int main()
{
using std::cout;

cout << "goodbye";
}

In real code its less typing, and IMO easier to read.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

P: n/a

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.130...
William Payne wrote in news:bv**********@news.island.liu.se:
Hello, I am starting to steer away from the practice of using "using
namespace std;" in my code. Instead I am qualifying each name in the
source when I use them, for example: std::cout << "Hello";

Now to my question. Depending upon the status of my program, I return
either EXIT_SUCCESS or EXIT_FAILURE from main(). Thinking that these
constants live in the std namespace, I tried:
return std::EXIT_FAILURE; but my compiler said:
server.cpp:116: error: parse error before numeric constant
So does that mean that all numeric constants in a namespace are
visible globally? Or are EXIT_FAILURE and EXIT_SUCCESS preprocessor
macros (#defines?) and therefore don't care about namespaces?


Yes, they're macros, the fact that they are all uppercase is a good hint
that that is true. Its only a hint though (std::FILE isn't a macro,
though IIUC in C FILE can be a macro).
I am
including <cstdlib>. Anything else I should think of when I want to be
careful and qualify each name where it's used? Any caveats?


Consider:

#include <iostream>
int main()
{
using std::cout;

cout << "goodbye";
}

In real code its less typing, and IMO easier to read.

Rob.
--
http://www.victim-prime.dsl.pipex.com/


Thanks alot for clearing that up, Rob. I am still a little puzzled by why
some names that are not macros are visible without qualification (at least
in my two compilers), but I am sure there is a reason for it other than bugs
in the implementation of the standard library. I recall hearing something
about "koenig lookup"...

/ William Payne
Jul 22 '05 #4

P: n/a
William Payne wrote in news:bv**********@news.island.liu.se:

Thanks alot for clearing that up, Rob. I am still a little puzzled by
why some names that are not macros are visible without qualification
(at least in my two compilers), but I am sure there is a reason for it
Many compilers implemented the <c....> style headers thus:

// part of cstdio:

#include <stdio.h>

namespace std
{
using ::printf;
}

This was just a hack and newer compilers are doing it right now.
other than bugs in the implementation of the standard library. I
recall hearing something about "koenig lookup"...


ADL (Argument Dependant Lookup) looks for function's in the namespaces
that the arguments its being called with were declared:

namespace A
{
struct B {};
void C( B ) {}
}

int main()
{
A::B b;
C( b ); // ADL
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.