Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #14657

Re: #include and namespaces

From "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: #include and namespaces
Date 2012-03-01 19:07 +0100
Organization A noiseless patient Spider
Message-ID <jiodtk$usl$1@dont-email.me> (permalink)
References <8266241.1084.1330590876018.JavaMail.geo-discussion-forums@vblb5> <jinri9$cet$1@dont-email.me> <jioc5q$h5i$1@dont-email.me>

Show all headers | View raw


On 01.03.2012 18:38, none Yannick Tremblay wrote:
> In article<jinri9$cet$1@dont-email.me>,
> Victor Bazarov<v.bazarov@comcast.invalid>  wrote:
>> On 3/1/2012 3:34 AM, Luca Cerone wrote:
>>> I've a header mylib.h
>>> that contains the line
>>>
>>> using namespace std ; // before the declaration of my namespace
>>> //and functions.
>>
>> That's A BAD IDEA(tm).
>>
>> Do not put 'using' directives in headers.
> [cut ...]
>> Write
>>
>>     using namespace std;
>>
>> in 'mylib.cpp' _only_.
>>
>>> but not in the main.cpp? (unless explicitely declared of course).
>
> Out of curiosity:
>
> Personally, I never use "using" for "std" even in cpp file.
> I will reject any code that put a "using namespace std;" directive in
> a header file. However what are peoples opinion of specific using
> directives in headers, e.g.: "using std::string;".  Should they be
> absolutely rejected as with "using namespace" or is there any
> situation under which they are tolerable?

It's pretty silly to make a rule against using the advantages of 
namespaces, just because some folks do something *similar* that's 
counter-productive.

In short, it is a good idea to rid the code of namespace qualifications 
so that it becomes *easy to read*. It should ideally read like normal 
English. Consider e.g. all the effort that Donald Knuth made for TeX 
(which Latex is a variant of), to make the code read just like English. 
You can dismiss my opinion here but if you dismiss the years Donald 
Knuth used on code readability then you're disqualifying yourself. So 
let's just agree that readable code is highly desirable, a main goal.

But while it is therefore a good idea to rid the code of namespace 
qualifications, it is decidedly not a good idea to add a host of 
potential *name collisions*, for example by adding the name `distance` 
to the global namespace (conflicts with `std::distance), or for example 
by adding `using namespace std` to the global namespace. One reasonable 
approach is to simply not do that. One downright stupid, not to say 
idiotic, approach is to free-associate and, due to such troubles, ban 
use of the name `distance` as well as `using namespace std;` in headers.


Cheers & hth.,

- Alf

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

#include and namespaces Luca Cerone <luca.cerone@gmail.com> - 2012-03-01 00:34 -0800
  Re: #include and namespaces Victor Bazarov <v.bazarov@comcast.invalid> - 2012-03-01 07:54 -0500
    Re: #include and namespaces Luca Cerone <luca.cerone@gmail.com> - 2012-03-01 06:38 -0800
      Re: #include and namespaces Victor Bazarov <v.bazarov@comcast.invalid> - 2012-03-01 14:26 -0500
        Re: #include and namespaces Luca Cerone <luca.cerone@gmail.com> - 2012-03-02 00:47 -0800
    Re: #include and namespaces yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-03-01 17:38 +0000
      Re: #include and namespaces Gareth Owen <gwowen@gmail.com> - 2012-03-01 18:07 +0000
        Re: #include and namespaces yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-03-01 18:52 +0000
          Re: #include and namespaces Paavo Helde <myfirstname@osa.pri.ee> - 2012-03-01 15:13 -0600
      Re: #include and namespaces "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-03-01 19:07 +0100
        Re: #include and namespaces yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2012-03-02 08:02 +0000
          Re: #include and namespaces "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-03-02 11:12 +0100
  Re: #include and namespaces Anand Hariharan <mailto.anand.hariharan@gmail.com> - 2012-03-01 11:20 -0800
    Re: #include and namespaces Victor Bazarov <v.bazarov@comcast.invalid> - 2012-03-01 14:30 -0500
      Re: #include and namespaces "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-03-01 20:53 +0100
        Re: #include and namespaces Miles Bader <miles@gnu.org> - 2012-03-02 09:51 +0900
        Re: #include and namespaces Daniel <danielaparker@gmail.com> - 2012-03-02 05:13 -0800
          Re: #include and namespaces Miles Bader <miles@gnu.org> - 2012-03-02 22:59 +0900
            Re: #include and namespaces Daniel <danielaparker@gmail.com> - 2012-03-02 10:31 -0800
              Re: #include and namespaces Miles Bader <miles@gnu.org> - 2012-03-03 10:54 +0900
              Re: #include and namespaces "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-03-03 08:53 +0100
  Re: #include and namespaces Juha Nieminen <nospam@thanks.invalid> - 2012-03-02 18:37 +0000
    Re: #include and namespaces Luca Cerone <luca.cerone@gmail.com> - 2012-03-03 04:22 -0800

csiph-web