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


Groups > comp.lang.c++ > #7278 > unrolled thread

Using "using" and namespace pollution from header files

Started byUrs Thuermann <urs@isnogud.escape.de>
First post2011-06-30 12:31 +0200
Last post2011-07-01 18:48 -0700
Articles 4 — 4 participants

Back to article view | Back to comp.lang.c++


Contents

  Using "using" and namespace pollution from header files Urs Thuermann <urs@isnogud.escape.de> - 2011-06-30 12:31 +0200
    Re: Using "using" and namespace pollution from header files Juha Nieminen <nospam@thanks.invalid> - 2011-06-30 11:00 +0000
      Re: Using "using" and namespace pollution from header files RaZiel <raziel_206_@hotmail.com> - 2011-07-02 03:25 +0200
      Re: Using "using" and namespace pollution from header files SG <s.gesemann@gmail.com> - 2011-07-01 18:48 -0700

#7278 — Using "using" and namespace pollution from header files

FromUrs Thuermann <urs@isnogud.escape.de>
Date2011-06-30 12:31 +0200
SubjectUsing "using" and namespace pollution from header files
Message-ID<ygf8vsj7gqy.fsf@janus.isnogud.escape.de>
I'd like to have some hints on how to best make use (or not make use)
of "using" in header files.  Say I have a header file like this:

	#include <list>
	#include <vector>
	
	class Foo {
	public:
	        std::list<int> foo();
	private:
	        std::vector<int> bar();
	};

I could add

        using std::list;
        using std::vector;

so I don't need the std:: prefix in the (maybe numerous) uses of list
and vector.  However, this causes the names to be visible for all
users of that header file, so I probably wouldn't do this for names
only needed in the private: section, like std::vector in the above
example.

For names needed in the public: section one could argue that the user
of the header file will probably also need std::list e.g. to define a
variable to store the return value from Foo::foo(), so the using
directive in the header problably doesn't hurt.

I still think it is better to avoid "using" at all in header files and
let the user decide what he wants in his namespace.

What is current or wide-spread best-practice?
	
urs

[toc] | [next] | [standalone]


#7280

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-06-30 11:00 +0000
Message-ID<4e0c5756$0$2818$7b1e8fa0@news.nbl.fi>
In reply to#7278
Urs Thuermann <urs@isnogud.escape.de> wrote:
> What is current or wide-spread best-practice?

  The most common coding guideline is that the 'using' keyword should
never be used in a header file.

  (Ok, the 'using' keyword has another role besides dealing with namespaces.
There are certain situations where you have to tell in a derived class that
a function in the base class should be visible in the derived class, but
that has nothing to do with namespaces. The guideline is only about bringing
names from namespaces to other namespaces.)

  Personally I follow the rule "never use 'using'" (except in those rare
cases where its role has nothing to do with namespaces, of course). I find
that the code becomes more readable and easier to understand with the
namespace prefixes than without them. For some strange reason most people
seem to think the opposite. I don't understand that.

[toc] | [prev] | [next] | [standalone]


#7341

FromRaZiel <raziel_206_@hotmail.com>
Date2011-07-02 03:25 +0200
Message-ID<iuls2e$tt5$1@speranza.aioe.org>
In reply to#7280
On 30.06.2011 13:00, Juha Nieminen wrote:
> Urs Thuermann<urs@isnogud.escape.de>  wrote:
>> What is current or wide-spread best-practice?
>
>    The most common coding guideline is that the 'using' keyword should
> never be used in a header file.
>
>    (Ok, the 'using' keyword has another role besides dealing with namespaces.
> There are certain situations where you have to tell in a derived class that
> a function in the base class should be visible in the derived class, but
> that has nothing to do with namespaces. The guideline is only about bringing
> names from namespaces to other namespaces.)
>
>    Personally I follow the rule "never use 'using'" (except in those rare
> cases where its role has nothing to do with namespaces, of course). I find
> that the code becomes more readable and easier to understand with the
> namespace prefixes than without them. For some strange reason most people
> seem to think the opposite. I don't understand that.

I agree with your last paragraph, but only when the length of the 
namespace prefixes is within a reasonable length.

[toc] | [prev] | [next] | [standalone]


#7343

FromSG <s.gesemann@gmail.com>
Date2011-07-01 18:48 -0700
Message-ID<02234fcb-3ebe-466f-8f2a-3be53fc568a0@e21g2000vbz.googlegroups.com>
In reply to#7280
On 30 Jun., 13:00, Juha Nieminen <nos...@thanks.invalid> wrote:
> Personally I follow the rule "never use 'using'" [in header files]

Same here. Though, my hope is that a proper module system solves this
issue.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web