473,385 Members | 1,357 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,385 software developers and data experts.

using directives

I remember reading an article (was it Herb Sutter's?) that recommended
avoiding using directives. While I quite understand this recommendation
for headers, what's wrong in using directive in .cpp files?

Apr 21 '06 #1
13 2729
rincewind wrote:
I remember reading an article (was it Herb Sutter's?) that recommended
avoiding using directives. While I quite understand this
recommendation for headers, what's wrong in using directive in .cpp
files?


Nothing. Just like there is nothing wrong with uninitialised pointers.
They both are just dangerous. Like loaded guns. Or double-edged swords.

The rule usually quoted in these contexts is "limit the scope of your
identifiers as closely as possible". A using directive explicitly goes
against that rule. You don't know what identifiers you bring to the
scope where the directive appears, you don't know how many you bring.
It's like tossing a heavy object up while standing in a crowd, hoping
that it misses everybody.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 21 '06 #2
"rincewind" <fa**@not.real> wrote in message
news:e2**********@gavrilo.mtu.ru
I remember reading an article (was it Herb Sutter's?) that recommended
avoiding using directives. While I quite understand this
recommendation for headers, what's wrong in using directive in .cpp
files?


In his (latest?) book with Andrei Alexandrescu, Herb Sutter does not
recommend against using directives in .cpp files, if indeed he ever did.

"You can and should use namespace using declarations and directives
liberally in your implementation files after #include directives and feel
good about it. Despite repeated assertions to the contrary, namespace using
declarations and directives are not evil and they do not defeat the purpose
of namespaces. Rather, they are what make namespaces usable...because they
greatly reduce code clutter by freeing you from having to tediously qualify
every name every time (which would be onerous, and frankly people just won't
put up with it) and still letting you qualify names only in those rare cases
when you need to resolve an actual ambiguity." (p. 108)

I should mention that they advocate always putting all code inside
namespaces, so that you do always have the option of fully qualifying names
in order to resolve ambiguities.
--
John Carson
Apr 22 '06 #3
John Carson wrote:
In his (latest?) book with Andrei Alexandrescu, Herb Sutter does not
recommend against using directives in .cpp files, if indeed he ever did.

"You can and should use namespace using declarations and directives
liberally in your implementation files after #include directives and feel
good about it. Despite repeated assertions to the contrary, namespace using
declarations and directives are not evil and they do not defeat the purpose
of namespaces. Rather, they are what make namespaces usable...because they
greatly reduce code clutter by freeing you from having to tediously qualify
every name every time (which would be onerous, and frankly people just won't
put up with it) and still letting you qualify names only in those rare cases
when you need to resolve an actual ambiguity." (p. 108)


Thanks for the quote! That's more or less what I was thinking. And
here's the article I was referring to:
http://www.gotw.ca/publications/migr...namespaces.htm. Quote from
this article: "Namespace Rule #1: Avoid using directives entirely,
especially in header files."
Apr 22 '06 #4
Victor Bazarov wrote:
The rule usually quoted in these contexts is "limit the scope of your
identifiers as closely as possible". A using directive explicitly goes
against that rule. You don't know what identifiers you bring to the
scope where the directive appears, you don't know how many you bring.

On the other hand, you can sometimes write a single using directive
instead of a dozen using declarations, making your program less verbose
- which I think is always a good thing to do. Unlike when in headers,
you don't get any unpredictable behaviour when you put this directive in
.cpp file.
Apr 22 '06 #5
rincewind wrote:
Victor Bazarov wrote:
The rule usually quoted in these contexts is "limit the scope of your
identifiers as closely as possible". A using directive explicitly
goes against that rule. You don't know what identifiers you bring
to the scope where the directive appears, you don't know how many
you bring.

On the other hand, you can sometimes write a single using directive
instead of a dozen using declarations, making your program less
verbose - which I think is always a good thing to do. Unlike when in
headers, you don't get any unpredictable behaviour when you put this
directive in .cpp file.


"Less verbose" is only good when you're interrogated by the enemy. In
programming, if your program is written in as few lines as you deem
possible, it's called "cryptic" or "obfuscated". One should always try
to achieve _clarity_, not minimal verbosity. A dozen using declarations
is definitely better than one sweeping using directive, in my book anyway.
At least you're being explicit and precise. Don't shoot a cannon where
a pin prick is sufficient.

V
--
Please remove capital As from my address when replying by mail
Apr 22 '06 #6
"rincewind" <fa**@not.real> wrote in message
news:e2***********@gavrilo.mtu.ru
John Carson wrote:
In his (latest?) book with Andrei Alexandrescu, Herb Sutter does not
recommend against using directives in .cpp files, if indeed he ever
did. "You can and should use namespace using declarations and directives
liberally in your implementation files after #include directives and
feel good about it. Despite repeated assertions to the contrary,
namespace using declarations and directives are not evil and they do
not defeat the purpose of namespaces. Rather, they are what make
namespaces usable...because they greatly reduce code clutter by
freeing you from having to tediously qualify every name every time
(which would be onerous, and frankly people just won't put up with
it) and still letting you qualify names only in those rare cases
when you need to resolve an actual ambiguity." (p. 108)


Thanks for the quote! That's more or less what I was thinking. And
here's the article I was referring to:
http://www.gotw.ca/publications/migr...namespaces.htm. Quote
from this article: "Namespace Rule #1: Avoid using directives
entirely, especially in header files."

Interesting. He has obviously changed his views somewhat.

--
John Carson
Apr 22 '06 #7
John Carson wrote:
Interesting. He has obviously changed his views somewhat.


Well I certainly haven't!!

http://www.google.com/search?q=phlip+namespace+tribbles

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 22 '06 #8
Phlip <ph******@yahoo.com> wrote:
John Carson wrote:
Interesting. He has obviously changed his views somewhat.

Well I certainly haven't!! http://www.google.com/search?q=phlip+namespace+tribbles


lol, thanks for letting us know ;)
--
jb

(reply address in rot13, unscramble first)
Apr 22 '06 #9
In article <Ns********************@comcast.com>,
v.********@comAcast.net says...

[...]
"Less verbose" is only good when you're interrogated by the enemy. In
programming, if your program is written in as few lines as you deem
possible, it's called "cryptic" or "obfuscated".
It's certainly true that attempting to make code
absolutely as compact as possible may tend toward
obfuscation. At that same time, verbosity can lead to an
obfuscation all its own -- even if the code is highly
readable on a microscopic scale, if the code is
sufficiently large, it can become difficult to understand
if for no other reason than the time taken to read such a
large amount. IMO, within almost any degree of reason and
sanity at all, more compact code is almost always more
readable unless you're simply dealing with something
that's quite trivial overall.
One should always try
to achieve _clarity_, not minimal verbosity. A dozen using declarations
is definitely better than one sweeping using directive, in my book anyway.
IMO, that depends. In the case of namespace std, I
generally agree. I'd even go so far as to say that any
using directive for namespace std should be considered
suspect at best. At the same time, in my own code I tend
to use quite a few relatively small namespaces in which
the entire contents are well-defined. In most cases, the
namespace is sufficiently cohesive that if you're going
to use them at all, you're probably going to use nearly
everything they expose, so you'll generally expose nearly
the same set of names either way.
At least you're being explicit and precise. Don't shoot a cannon where
a pin prick is sufficient.


People routinely talk about being explicit as if it were
a good thing. I generally believe rather the opposite is
true. To the extent that's reasonable, the more that can
be made implicit the better. People have a relatively low
limit on how much information they can keep in mind
currently at any given time. Making more details explicit
forces the programmer to keep more of those details in
mind to use the code. This, in turn, directly limits the
programmer's ability to understand and reason about the
code.

The trick is making the right things implicit. If you
attempt to make something implicit that the programmer
will need to think about anyway, that causes a problem.
If you choose the right things to make implicit, that
tends to improve readability and understandability.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Apr 22 '06 #10
"John Carson" <jc****************@netspace.net.au> wrote:
"rincewind" <fa**@not.real> wrote in message
news:e2***********@gavrilo.mtu.ru
John Carson wrote:
In his (latest?) book with Andrei Alexandrescu, Herb Sutter does not
recommend against using directives in .cpp files, if indeed he ever
did. "You can and should use namespace using declarations and directives

[...]

Note that you snipped that part that says not to use them in .cpp files
_before an #include_. That's why the title of Item 59 is:

59. Don't write namespace usings in a header file or before an #include.

The complete rule is not to write them where they can affect someone
else's code, which means a) not in any header file, and b) not in a .cpp
file before an #include (else they can affect the meaning of the code in
that later-included header).

Herb

---
Herb Sutter (www.gotw.ca) (www.pluralsight.com/blogs/hsutter)

Convener, ISO WG21 (C++ standards committee) (www.gotw.ca/iso)
Architect, Developer Division, Microsoft (www.gotw.ca/microsoft)
Apr 25 '06 #11
"Herb Sutter" <hs*****@gotw.ca> wrote in message
news:q0********************************@4ax.com
"John Carson" <jc****************@netspace.net.au> wrote:
"rincewind" <fa**@not.real> wrote in message
news:e2***********@gavrilo.mtu.ru
John Carson wrote:
In his (latest?) book with Andrei Alexandrescu, Herb Sutter does
not recommend against using directives in .cpp files, if indeed he
ever did. "You can and should use namespace using declarations and
directives [...]

Note that you snipped that part that says not to use them in .cpp
files _before an #include_. That's why the title of Item 59 is:


No I didn't. You are the snipper, not me :) Read again.
59. Don't write namespace usings in a header file or before an
#include.

The complete rule is not to write them where they can affect someone
else's code, which means a) not in any header file, and b) not in a
.cpp file before an #include (else they can affect the meaning of the
code in that later-included header).

Herb

---
Herb Sutter (www.gotw.ca) (www.pluralsight.com/blogs/hsutter)

Convener, ISO WG21 (C++ standards committee) (www.gotw.ca/iso)
Architect, Developer Division, Microsoft (www.gotw.ca/microsoft)

--
John Carson

Apr 25 '06 #12
"John Carson" <jc****************@netspace.net.au> wrote in message
news:44***********************@un-2park-reader-01.sydney.pipenetworks.com.au
"Herb Sutter" <hs*****@gotw.ca> wrote in message
news:q0********************************@4ax.com
"John Carson" <jc****************@netspace.net.au> wrote:
"rincewind" <fa**@not.real> wrote in message
news:e2***********@gavrilo.mtu.ru
John Carson wrote:
> In his (latest?) book with Andrei Alexandrescu, Herb Sutter does
> not recommend against using directives in .cpp files, if indeed he
> ever did. "You can and should use namespace using declarations and
> directives [...]


Note that you snipped that part that says not to use them in .cpp
files _before an #include_. That's why the title of Item 59 is:


No I didn't. You are the snipper, not me :) Read again.


A clarification: I included the statement that you should use using
directives "after #include directives", but did omit the discussion that
follows which explains at length why putting them before #include directives
is a bad thing.

--
John Carson
Apr 25 '06 #13
John Carson wrote:
"Herb Sutter" <hs*****@gotw.ca> wrote in message
news:q0********************************@4ax.com
"John Carson" <jc****************@netspace.net.au> wrote:
"rincewind" <fa**@not.real> wrote in message
news:e2***********@gavrilo.mtu.ru
John Carson wrote:
> In his (latest?) book with Andrei Alexandrescu, Herb Sutter does
> not recommend against using directives in .cpp files, if indeed he
> ever did. "You can and should use namespace using declarations and
> directives [...]

Note that you snipped that part that says not to use them in .cpp
files _before an #include_. That's why the title of Item 59 is:


No I didn't. You are the snipper, not me :) Read again.


John, I think Herb knows what he wrote in his book.
Apr 25 '06 #14

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

Similar topics

17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
3
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use...
30
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
4
by: Chuck Anderson | last post by:
My Web Host (Linux - Apache) recently upgraded to Php5. One thing lost in the upgrade was a global configuration that allowed me to place php.ini files in any directory and they would be read and...
7
by: patrick | last post by:
Why do some code listings for learning C++ have the entire namespace std being used while others just specify the parts they want to use?
7
by: =?Utf-8?B?QmVu?= | last post by:
Hi, I sometimes see code where the 'using' statements at the top of the file are located before the namespace declaration, and sometimes they are located inside it. For example: using...
6
by: Daniel Kraft | last post by:
Hi all, in my C++ projects, I usually make "heavy use" of namespaces to "encapsulate" my code. When I have for instance something like this: namespace project { namespace part1 { ... }...
76
by: jacob navia | last post by:
Since standard C doesn't provide any way for the programmer to direct the compiler as to how to layout structures, most compilers provide some way to do this, albeit in different forms. ...
14
by: lagman | last post by:
All, I'm looking for a tool that is able to take my code base (which is full of preprocessor directives) and spit out source code that is "clean" of any preprocessor directives based on the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.