23

I understand the troubles you can get into when you put a using declaration inside a header file, so I don't want to do that. Instead I tried to put the using (or a namespace foo =) within the class declaration, to cut down on repetitive typing within the header file. Unfortunately I get compiler errors. Seems like it would be a useful feature.

#ifndef FOO_H
#define FOO_H

// This include defines types in namespace gee::whiz::abc::def,
// such as the class Hello.
#include "file_from_another_namespace.h"

// using namespace gee::whiz::abc::def; // BAD!

namespace x {
   namespace y {
      namespace z {

struct Foo {
    using namespace gee::whiz::abc::def; // Illegal.
    namespace other = gee::whiz::abc::def; // Illegal.

    // Foo(gee::whiz::abc::def::Hello &hello); // annoyingly long-winded

    Foo(other::Hello &hello); // better
    //...
};

} } } // end x::y::z namespace

#endif // FOO_H

In the real code, the namespace names are much longer and annoying and it's not something I can change.

Can anyone explain why this is not legal, or (better) if there's a workaround?

Dan
  • 5,929
  • 6
  • 42
  • 52

3 Answers3

26

Could you do typedef gee::whiz::abc::def::Hello Hello?

Fredrik Ullner
  • 2,106
  • 2
  • 22
  • 28
  • 2
    This will still pollute the namespace which is what he is trying to avoid. – Trent Jan 25 '10 at 19:22
  • 8
    Not if its being used inside the class declaration. – Georg Fritzsche Jan 25 '10 at 19:25
  • 1
    Yeah that works! And if I make it a private typedef then other code can't use it accidentally. – Dan Jan 25 '10 at 19:32
  • @gf, you're right, if the typedef is inside the class declaration it would work. BTW, I can't change my down-vote to an up-vote even though it was my mistake unless the answer changes :( – Trent Jan 25 '10 at 19:32
  • @Trent: You can always undo a down-vote by clicking the down button again. – jamesdlin Jan 25 '10 at 19:57
  • @jamesdlin, no I can't, the message when I try to undo the down-vote or change it to an up-vote is: "Vote too old to be changed, unless this answer is edited". I supposed 'I' could edit the answer, then change my vote :) – Trent Jan 25 '10 at 20:58
4

actually not a totally horrid idea. It makes at least as much sense as how it works now (which granted, isn't much). I think the basic problem is that classes are not the unit of compilation and linking, but 'translation units'. But doing it class by class is much cleaner, having classes be modules, like in Java or C# or other languages that make more sense.

1

Had the same problem, found this question. I figured out that if you wrap struct foo with an anonymous namespace, it seems you can put

using namespace too::many::names;

at the top of the anonymous wrapper. It's kind of ugly, though, adding more layers of nested braces.

Allan Stokes
  • 515
  • 6
  • 5
  • Hmm... but then you have an anon namespace in a header file. Not sure I want to open that can of worms: http://stackoverflow.com/questions/357404/anonynous-namespaces http://stackoverflow.com/questions/357564/uses-for-anonymous-namespaces-in-header-files – Dan Dec 03 '10 at 05:01
  • I understand that the design of namespaces was one of the regrets from a previous standardization round, since it was crammed in without much implementation experience. I've mostly been using Boost header libraries lately, so this seemed to work OK in a five minute experiment. – Allan Stokes Dec 04 '10 at 06:28
  • For some reason, the add comment is being triggered during normal keyboarding. Twice. The link above suffers from a Socratic straw man: the guru is so busy showing off than none of the pipsqueaks challenges the rational for not allow using directives in class scope. Last night I read the paper Bjarne wrote hand wringing about concepts, and felt illuminated on both sides. From the above link, not so much. What if I don't want my source code to look like a SOAP datagram with tediously qualified redundancy? Huh huh huh? All pitfalls and no flashlight. – Allan Stokes Dec 04 '10 at 06:43