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


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

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 20:53 +0100
Organization A noiseless patient Spider
Message-ID <jiok36$77m$1@dont-email.me> (permalink)
References <8266241.1084.1330590876018.JavaMail.geo-discussion-forums@vblb5> <a75a3f81-aaf4-46e8-8d17-f53c505ae8e1@s7g2000yqm.googlegroups.com> <jioioq$ts9$1@dont-email.me>

Show all headers | View raw


On 01.03.2012 20:30, Victor Bazarov wrote:
> On 3/1/2012 2:20 PM, Anand Hariharan wrote:
>> On Mar 1, 2:34 am, Luca Cerone<luca.cer...@gmail.com> wrote:
>>> Dear all,
>>> I've a header mylib.h
>>> that contains the line
>>>
>>> using namespace std ; // before the declaration of my namespace
>>> //and functions.
>>>
>>> I put this because I'm declaring several functions that use vectors
>>> and strings and I'd like not to write std:: in front of every of such
>>> types.
>>>
>>> Everything works fine, but there is a small issue:
>>
>> As others have said, it is a bad idea. What is a small issue now,
>> will become a bigger issue as your program grows as well as when the
>> library grows with more names introduced into the std namespace.
>>
>> If you use only vector or string, you could do --
>>
>> using std::string;
>> using std::vector;
>>
>> While the name 'string' being introduced into all your C++ source
>> files should not cause a problem, introducing the name 'vector' could.
>>
>> The simplest solution is to do a search-replace of string or vector
>> with std::string or std::vector in your header file and you should be
>> able to resolve your problem.
>
> For toy projects I consider it totally acceptable to even do
>
> typedef std::vector<int> vecint;
> typedef std::vector<double> vecdbl;
> typedef std::string str;
>
> _in a header_ and use 'vecint', 'vecdbl' and 'str' afterwards. However,
> it all hinges on the definition of "a toy project".

More importantly, the thing that matters is whether it is in the global 
namespace.

To do things properly, just do

   #pragma once
   namespace realProject {
       using std::vector;

       // Blah blah

   }  // namespace realProject

I would be wary of "using namespace std;" even in a custom namespace, 
because anyone "using yourCustomNamespace;" could then get in trouble. 
But the ability to resolve any collisions is also part of the advantages 
of namespaces. So I think a ban is counter-productive, and in general, 
any mechnical rule meant to replace the application of intelligence and 
common sense, is in my view worse than what it cures.

Cheers,

- Alf  (perhaps dissenting, somewhat)

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