473,569 Members | 3,054 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_FAILU RE; 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 1970

"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_FAILU RE; 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_FAILU RE; 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.RE MOVE.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_FAILU RE; 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
1861
by: Raymond Hettinger | last post by:
Comments are invited on the following proposed PEP. Raymond Hettinger ------------------------------------------------------- PEP: 329
4
2695
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
6327
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 different enum definitions? I think it should have been perfectly valid to do that. Its a stupid limitation to have to define disjoint enum symbol...
8
2875
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 class that I want its instance to be accessible from any other classe in the project. There is also a bunch of constants that I want to be public...
3
1159
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 Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
6
3117
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 database. In times recently past, I would have done this with Borland Delphi. So, that's my perspective and I have my old Delphi code to crib from. ...
4
11171
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 // Does 'F' mean float here?
2
2505
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 this: template<bool DoublePrecision> struct Constants { typedef double SampleType; static const double pi;
54
3559
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 file writing: const double PI = 3.14; Every time using it, include the header file.
0
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3662
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.