Are numeric constants in a namespace visible globally?

W

William Payne

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
 
S

Sharad Kala

William Payne said:
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
 
R

Rob Williscroft

William Payne wrote in
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.
 
W

William Payne

Rob Williscroft said:
William Payne wrote in

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).


Consider:

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

cout << "goodbye";
}

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

Rob.

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
 
R

Rob Williscroft

William Payne wrote in
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top