473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

header file and 'using directive'

Suppose the following is in Test.h

#ifndef TEST_H
#define TEST_H

#include <iostream>
#include <string>

using namespace std;

class Test
{
public:
Test(const string& str) : val(str)
{
cout << val << endl;
}

string val;
};
#endif

The above is accepted by the compiler. But generally 'using
declaration/directives' are not mentioned in header files. Why is it
so ?

Thanks
V.Subramanian
Jan 5 '08 #1
11 2499
su**************@yahoo.com, India wrote:
Suppose the following is in Test.h

#ifndef TEST_H
#define TEST_H

#include <iostream>
#include <string>

using namespace std;
Don't.

You force the using directive on any file that includes the header.

Why to people get so obsessed with "using namespace std", rather than
simply using fully qualified names?

--
Ian Collins.
Jan 5 '08 #2
<su**************@yahoo.comwrote in message
news:a1**********************************@d21g2000 prf.googlegroups.com...
: Suppose the following is in Test.h
:
: #ifndef TEST_H
: #define TEST_H
:
: #include <iostream>
: #include <string>
:
: using namespace std;
:
: class Test
: {
: public:
: Test(const string& str) : val(str)
: {
: cout << val << endl;
: }
:
: string val;
: };
: #endif
:
: The above is accepted by the compiler. But generally 'using
: declaration/directives' are not mentioned in header files.
: Why is it so ?

Because they go against the purpose of namespaces, creating
potential name collisions in all compilation units that
include the header.

Consider the following two example files:

//BobsLib.h
namespace BobsLib {
class runtime_error { ... };
...
}
//MySourceFile.cpp
#include "BobsLib.h"
#include "Test.h" // your file above

// My file intensively uses BobsLib
using namespace BobsLib;

void myFunction()
{
try {
//stuff using BobsLib...
}
catch( runtime_error& x ); //## ouch
// runtime_error ambiguous: std::~ or BobsLib::~ ?
}
Basically, but putting a using directive in the global
scope of a header file, you prevent all users of that
header from safely using that feature in their own code.
Not nice.
I hope this helps,
Ivan

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Jan 5 '08 #3
On 5 ÑÎ×, 15:44, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
The above is accepted by the compiler. But generally 'using
declaration/directives' are not mentioned in header files. Why is it
so ?
Because when you include header file in your source code (that's the
same as copy-pasting whole header file there), the using directive
appears there as well, making std namespace unexpectedily visible to
your statemsnts without any qualifying.
Jan 5 '08 #4
"Ian Collins" writes:
Why to people get so obsessed with "using namespace std", rather than
simply using fully qualified names?
Because doing the fully qualified things leads to an annoying thicket of
syntactic clutter. It's like talking to someone who peppers his
conversation liberally with "you know", of which there are tons. Some
people have minds apparently capable of effortlessly filtering out all kinds
of noise, some minds don't work that way. For the latter, the using
directive makes life bearable. Of course it must be used properly, which is
usually a good idea with almost any tool.

std::That std::is std::the std::only ::std::reason std::for std::the
std::usage std::you std::dislike std.
Jan 5 '08 #5
On Jan 5, 3:46 pm, "osmium" <r124c4u...@comcast.netwrote:
"Ian Collins" writes:
Why to people get so obsessed with "using namespace std", rather than
simply using fully qualified names?
Because doing the fully qualified things leads to an annoying
thicket of syntactic clutter. It's like talking to someone
who peppers his conversation liberally with "you know", of
which there are tons. Some people have minds apparently
capable of effortlessly filtering out all kinds of noise, some
minds don't work that way.
I think the problem is in that statement. Some people may
consider it noise, but as far as I'm concerned, the name of the
object is std::cout. The std:: is no more "noise" than is the
"Kanze" in my name. There are contexts where just using "James"
when talking about me may be acceptable, but in general, if you
want your reader to know exactly who you're talking about,
you'll use "James Kanze". Or std::cout.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 5 '08 #6
On Jan 5, 12:05 pm, "Alf P. Steinbach" <al...@start.nowrote:
* subramanian10...@yahoo.com, India:
[...]
class Test
{
public:
Test(const string& str) : val(str)
{
cout << val << endl;
}
Breaks the separation-of-concerns design rule. As a general
rule, don't do i/o in constructors. And don't do i/o in
functions or classes that aren't specifically concerned with
i/o.
That probably applies to this type of I/O (and it certainly
applies to std::cout), but I regularly use constructors which
construct objects from persistent store. I think it's a
standard technique when objects should be persistent.

Logging is also appropriate in constructors for application
level classes. Logging definitly involves I/O as well (although
it shouldn't be as direct as this).
The main rationale is the same as for the using directive,
namely, to not force things on client code.
E.g. stream i/o may not be welcome in a GUI application.
In addition, you don't have much control over where
constructors are called (or not).
That's true for objects with value semantics (and then probably
only for the copy constructor), but again, there are large
classes of objects which don't support copy construction. And
of course, the constructor which reads from persistent store is
probably called with the input stream as an argument---you
certainly have total control over where it is called.
Thus, i/o or other side-effects in a constructor is inherently
unreliable.
When reading from persistent store, that's what exceptions are
for (although in specific cases, other strategies may also be
appropriate). When writing to a log, if the write fails, it
fails---your application still works.

Note well that I don't approve outputting to std::cout in a
constructor, at least in production code. I can't ever think of
a case in production code where that would appropriate. (On the
other hand, if you want to write a quick test to see whether
your compiler does NRVO in a specific case...)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 5 '08 #7
Ian Collins <ia******@hotmail.comwrote:
su**************@yahoo.com, India wrote:
Suppose the following is in Test.h

#ifndef TEST_H
#define TEST_H

#include <iostream>
#include <string>

using namespace std;
Don't.

You force the using directive on any file that includes the header.

Why to people get so obsessed with "using namespace std", rather than
simply using fully qualified names?
Because, refusing to use the using directive at all goes against the
purpose of namespaces. It puts us back in the bad old days when we had
to put nonsense letters at the beginning of every identifier to save us
from package collisions.
Jan 5 '08 #8
Daniel T. wrote:
Ian Collins <ia******@hotmail.comwrote:
>su**************@yahoo.com, India wrote:
>>Suppose the following is in Test.h

#ifndef TEST_H
#define TEST_H

#include <iostream>
#include <string>

using namespace std;
Don't.

You force the using directive on any file that includes the header.

Why to people get so obsessed with "using namespace std", rather than
simply using fully qualified names?

Because, refusing to use the using directive at all goes against the
purpose of namespaces. It puts us back in the bad old days when we had
to put nonsense letters at the beginning of every identifier to save us
from package collisions.
But "using namespace std" appears to have become automatic for some,
which also goes against the purpose of namespaces! If you are going to
wite this in every file, why bother with the std namespace at all?

--
Ian Collins.
Jan 5 '08 #9
Ian Collins <ia******@hotmail.comwrote:
Daniel T. wrote:
Ian Collins <ia******@hotmail.comwrote:
Why to people get so obsessed with "using namespace std", rather than
simply using fully qualified names?
Because, refusing to use the using directive at all goes against the
purpose of namespaces. It puts us back in the bad old days when we had
to put nonsense letters at the beginning of every identifier to save us
from package collisions.

But "using namespace std" appears to have become automatic for some,
which also goes against the purpose of namespaces!
I don't agree. The purpose of namespaces is so that I can use two
libraries that were developed independently of each other, without
having to worry about collisions between those libraries. For example,
if I am using the barney library, and the fred library and they both
have a "Foo" identifier, the fact that they are in in different
namespaces will save me a whole lot of grief. If I'm not using two
different libraries that were developed independently of each other,
then a local using-directive is fine.
Jan 6 '08 #10
"Daniel T." <da******@earthlink.netwrites:
Ian Collins <ia******@hotmail.comwrote:
>su**************@yahoo.com, India wrote:
Suppose the following is in Test.h

#ifndef TEST_H
#define TEST_H

#include <iostream>
#include <string>

using namespace std;
Don't.

You force the using directive on any file that includes the header.

Why to people get so obsessed with "using namespace std", rather than
simply using fully qualified names?

Because, refusing to use the using directive at all goes against the
purpose of namespaces.
Please re-read the thread subject and above messages. No one is suggesting
not to use using *at all*, just pointing out that it shouldn't be used in
a *header file*.

Using it in a .cpp file, where the scope of its effect is limited to the
file its in, is a different question entirely.

sherm--

--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net
Jan 6 '08 #11
Sherman Pendley <sp******@dot-app.orgwrote:
"Daniel T." <da******@earthlink.netwrote:
Ian Collins <ia******@hotmail.comwrote:
Why to people get so obsessed with "using namespace std",
rather than simply using fully qualified names?
Because, refusing to use the using directive at all goes against
the purpose of namespaces.

Please re-read the thread subject and above messages. No one is
suggesting not to use using *at all*, just pointing out that it
shouldn't be used in a *header file*.
Some suggest that using-directives, even in the .cpp file, are a bad
idea. I was merely covering all the bases.
Using it in a .cpp file, where the scope of its effect is limited
to the file its in, is a different question entirely.
I agree. Funny though, where I work I am the only one who refuses to put
using-directives in headers. None of the programmers who put them in the
headers have ever been burned by it... They think I'm silly.
Jan 6 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Steven T. Hatton | last post by:
If a header is not necessarily a source file, and the sequences delimited by < and > in header names aren't necessarily valid source file names, what exactly is a header? -- p->m == (*p).m == p.m...
2
by: trying_to_learn | last post by:
im in the primary stages of learning C++. The book im learning from says Dont use using namespace.. directive in header file However im trying to make the following header file.I need to include...
11
by: ambika | last post by:
Iam just trying to know "c". And I have a small doubt about these header files. The header files just contain the declaration part...Where is the definition for these declarations written??And how...
60
by: Derrick Coetzee | last post by:
It seems like, in every C source file I've ever seen, there has been a very definite include order, as follows: - include system headers - include application headers - include the header...
4
by: Andrew Ward | last post by:
Hi All, I was wondering if it is possible to use precompiled headers without having to include a <stdafx.h> or whatever in every source file. My problem is that I have a project that makes heavy...
6
by: Jofio | last post by:
I have a .h (header file) linked using #include<...>. I also have a .cp file which i don't know how to include in the header of my "main" file. I have the following c++ file. The file names are:...
12
by: bgeneto | last post by:
I know that it's a very basic question, but I can't figure out or find an answer to why do we have to specify a namespace, like this #include<string> using namespace std; when using the...
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
7
by: bcpkh | last post by:
Hello All Received a header file from a supplier that defines an interface to implement but it's giving me a problem, I reproduce the general structure of the header file below; #ifndef XYZ_H...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.