Thread: C++: (ab)using?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262

    C++: (ab)using?

    Hello all,

    I have a question that is just a matter of opinion. I would like to see people explain their point of view on the subject: should one use the keyword "using", or should one preface a namespace every time something in it is referenced? And if you're in favor of using using, do you use "using namespace" or just the referenced item, eg. "using std::cout"?

    Myself, I don't use "using" in any case (with the exception of programming contests, where speed is more important than good code). I find that it entirely kills the entire use of namespaces. I barely use "using", and yet the few times I did use it I ran into trouble, the only one I can remember now was "int map[25][25];". And somehow I find it tells the reader of the code a lot more on the object to specify, each time, where this object is located.
    Honestly, I'm quite surprised how many people use using, and the fact that that keyword even exists.

    So, what are your guys' thoughts?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Any of those are fine. The crux of the problem with using namespace statements is not necessarily what they do but the scope in which they are applied.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by EVOEx
    Myself, I don't use "using" in any case (with the exception of programming contests, where speed is more important than good code). I find that it entirely kills the entire use of namespaces. I barely use "using", and yet the few times I did use it I ran into trouble, the only one I can remember now was "int map[25][25];". And somehow I find it tells the reader of the code a lot more on the object to specify, each time, where this object is located.
    In C++ Coding Standards, Sutter and Alexandrescu argue the opposite:
    "But the vast majority of the time there is no ambiguity: And that is why namespace using declarations and directives 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."

    Personally, I would rather only use using declarations and using directives when in some restricted scope as I find that it is easier to spot those "rare cases" that way.

    Quote Originally Posted by EVOEx
    Honestly, I'm quite surprised how many people use using, and the fact that that keyword even exists.
    There are uses for using declarations that are beyond any argument of convenience, e.g., it is used to bring in names from a base class that are otherwise hidden by an overload in a derived class.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I usually use using namespace std; since I know better than to use variable names that are identical to STL names. I very rarely run into a conflict; but when I do, I just explicitly qualify the name with std::

    But of course when I write a header file, I never put a using declaration there because it's just an evil thing to do to other people that use your header file...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think there are cases when you'd like to use "using namespace X", and other times when using "X::".

    As to the cases where it causes conflicts, "int map[...]" is not causing a big problem - either rename the variable [you are compiling often, aren't you? So changing the variable name would be little work] - or prefix it to the global namespace: "int ::map[...]" whenever it is used.

    The worst kind of problem is when there is a silent "failure":
    Code:
    int cin;
    ... 
    int x = 0;
    
    cin >> x;
    This would compile and link fine, and even run without any problems at all - it just wouldn't do what you expected...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >should one use the keyword "using", or should one preface
    >a namespace every time something in it is referenced?
    That's a matter of personal preference. I tend to prefer the prefix except when my lines start to get longer than I'm comfortable with, and then I restrict the scope of the using directive as much as possible.

    >do you use "using namespace" or just the referenced item, eg. "using std::cout"?
    I've never needed the added control of the using declaration. In my opinion, it just adds clutter compared to intelligent use of the using directive.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> In C++ Coding Standards, Sutter and Alexandrescu argue the opposite

    My personal opinion is that there argument is more applicable to longer and more detailed namespaces other than std, and/or when the programmer is making extensive use of namespaces. All beginners and many experienced C++ programmers work in an environment where std is the only namespace to worry about. In cases where you're juggling many namespaces that are more cumbersome to type and harder to read than std, then the greater use of using directives and declarations seems to make more sense.

Popular pages Recent additions subscribe to a feed