By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

using directives

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.