473,507 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Are numeric constants in a namespace visible globally?

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
4 1963

"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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1858
by: Raymond Hettinger | last post by:
Comments are invited on the following proposed PEP. Raymond Hettinger ------------------------------------------------------- PEP: 329
4
2690
by: Teddy | last post by:
If I use just a STL container such as std::vector in my program. Is "using std::vector;" better than "using namespace std" ? Does "using namespace std" cost ?
21
6319
by: Bilgehan.Balban | last post by:
Hi, I have two different enum definitions with members of same name. The compiler complains about duplicate definitions. Is this expected behaviour? Can't I have same-named fields in...
8
2866
by: Marty | last post by:
Hi, I'm new to C#, I used to code in VB.NET. Where is the best place to declare all my constants and global objects in my C# project to have them accessible globally? I have an event logger...
3
1150
by: Trint Smith | last post by:
I have several strings in my project that I want all programs in the project to be able to see...How do I do that? Thanks, Trint .Net programmer trintsmith@hotmail.com *** Sent via...
6
3113
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient...
4
11149
by: Ioannis Vranos | last post by:
Consider the following uses of 0 as constants: float f= 0f; // doesn't seem to work float f= 0.f // Is this valid? float f= 0.F // Is this valid? float f= 0.0f; // It works float f= 0.0F...
2
2498
by: Leslie Sanford | last post by:
I want to define a set of floating point constants using templates insteand of macros. I'd like to determine whether these constants are floats or doubles at compile time. In my header file, I have...
54
3550
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7114
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...
0
7377
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...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7488
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...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
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...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.