Thread: Which way is better ?

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is fine. I am merely pointing out that you shouldn't blindly put using namespace statements for every namespace you encounter.
    Especially phantomotap seemed to think that explicitly specifying namespaces was like the old C days when you had to spell it all out. Or at least it seemed that way.
    I didn't agree, because I use namespaces for categorizing. And the using isn't evil; there are places where it helps, like in your example, and others where it does not.
    Myself, if it is not too long, I do prefer to type it out explicitly.
    Last edited by Elysia; 02-10-2009 at 10:47 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'm just saying one [...] not back in the C era.
    O_o

    You are confusing "may" with "must".

    If one "may" fully qualify names, for whatever reason, it isn't bad at all.

    If one "must" fully qualify names, the namespaces are being used solely as a prefix. All the advantages of namespaces are lost. There is no difference between one prefix notation and the other. The source may only use ':' instead of '_'.

    Which is more readable?

    Code:
    std__copy(std__istream_iterator<std__string>(std__cin), std__istream_iterator<std__string>(), std__ostream_iterator<std__string>(std__cout, " "));
    Code:
    // using directives
    copy(istream_iterator<string>(cin), istream_iterator<string>(), ostream_iterator<string>(cout, " "));
    Soma
    Last edited by phantomotap; 02-10-2009 at 10:59 AM.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by whiteflags
    Namespaces are there to resolve things when the same identifier has been used multiple times, and the above communicates to me that namespaces somehow lose their value when they fulfill their purpose.
    I think you have made a strawman argument since phantomotap compared namespaces to name prefixes, and it is in that context that he stated that "all the advantages of namespaces are lost" (emphasis mine).
    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. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    *shrug* I deleted my comment because it occurred to me that Soma was probably saying what I was saying, in so many words. That happens. I'd ignore him so that I don't bug him but he's actually smart, says great advice, and that's mean.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by phantomotap View Post
    O_o

    You are confusing "may" with "must".

    If one "may" fully qualify names, for whatever reason, it isn't bad at all.

    If one "must" fully qualify names, the namespaces are being used solely as a prefix. All the advantages of namespaces are lost. There is no difference between one prefix notation and the other. The source may only use ':' instead of '_'.

    Which is more readable?

    Code:
    std__copy(std__istream_iterator<std__string>(std__cin), std__istream_iterator<std__string>(), std__ostream_iterator<std__string>(std__cout, " "));
    Code:
    // using directives
    copy(istream_iterator<string>(cin), istream_iterator<string>(), ostream_iterator<string>(cout, " "));
    Soma
    Ah, yes. You are right. Still, I prefer
    Code:
    std::copy(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>(), std::ostream_iterator<std::string>(std::cout, " "));
    Not only do they avoid collisions (which is pretty irrelevant in this example, but still), but I can see where they come from, and I do like to prefix this explicitly, unless too long.
    Now, those are good arguments to ponder. When to use using namespace directories and when not to?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'd ignore him so that I don't bug
    ^_^

    You shouldn't bother about that; everyone bugs me anyway.

    When to use using namespace directories and when not to?
    Beyond rules that prevent pollution or allow overload resolution, this seems to fall under the realm of style.

    Soma

Popular pages Recent additions subscribe to a feed