A quick question

This is a discussion on A quick question within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by laserlight I agree, but I am also persuaded by Sutter and Alexandrescu's argument that "the vast majority ...

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by laserlight View Post
    I agree, but I am also persuaded by Sutter and Alexandrescu's argument that "the vast majority of the time there is no ambiguity".
    At the risk of turning this into a "my expert is bigger than yours" contest, search groups.google.com and you'll see comments by Andrew Koenig and Bjarne Stroustrup that argue against such liberal use of using directives, as they defeat the purpose of namespaces in the first place.

    Anyway, opinions certainly differ on the matter.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by medievalelks View Post
    ... as they defeat the purpose of namespaces in the first place.
    I don't think that's true.
    A namespace was designed to be able to organize data and functions better, not hide them away from each other so you can use the same name for different functions.
    In other words, it simply allows you to organize data. Say it's a string function, put it in namespace Strings.
    Theoretically, you should be able to import them directly into the global namespace just because namespaces are only organizing names to group functions.
    The problems shows up when you multiple functions with the same name, which is why it's to be avoided.

    Myself I tend to use full names just to show that this is a string function, for example.
    It also allows me to remove certain stuff from the function name, such as instead of SplitString, it can be named Split because it's in the Strings namespace.
    Last edited by Elysia; 04-20-2008 at 10:25 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.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    At the risk of turning this into a "my expert is bigger than yours" contest, search groups.google.com and you'll see comments by Andrew Koenig and Bjarne Stroustrup that argue against such liberal use of using directives, as they defeat the purpose of namespaces in the first place.
    "Infinite are the arguments of mages."
    - Vetch, from A Wizard of Earthsea, by Ursula le Guin

    Anyway, opinions certainly differ on the matter.
    That is precisely what I was saying in the first place.

    I happen to take the same stance as you do (Sutter and Alexandrescu's stance + only use within a local scope), but it is fun to play Devil's Advocate, when one of the "devils" is chair of the C++ Standards committee

    EDIT:
    A namespace was designed to be able to organize data and functions better.
    I think all four experts quoted on this page disagree and believe that namespaces are designed merely to manage names, not organise data and functions (which would be the purposes of classes, methinks).
    Last edited by laserlight; 04-20-2008 at 10:24 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Elysia View Post
    I don't think that's true.
    A namespace was designed to be able to organize data and functions better, not hide them away from each other so you can use the same name for different functions.
    Right, and by putting "using namespace x;" at the top of all your source files, you've essentially put x's names right back into the global namespace. Putting them at function scope, however, introduces less risk. using declarations (like "using std::string") is even less risky.

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by laserlight View Post
    I think all four experts quoted on this page disagree and believe that namespaces are designed merely to manage names, not organise data and functions (which would be the purposes of classes, methinks).
    It's how I see it anyway. I always try to organize my code library into namespaces to make it look more ala .NET.
    Functions are properly grouped into proper namespaces, making it easier to distinguish the purpose of functions and avoid making too long or repeatable names.

    Quote Originally Posted by medievalelks View Post
    Right, and by putting "using namespace x;" at the top of all your source files, you've essentially put x's names right back into the global namespace. Putting them at function scope, however, introduces less risk. using declarations (like "using std::string") is even less risky.
    That wasn't my point. They are categorized, but that shouldn't mean you have to explicitly mention the category names when using the functions. You can simple tell the compiler "Hey, I want to use this category, can you fix it for me, please?" and then just use the functions.
    The only drawback is that it introduces name conflicts, which is why it should be avoided.
    But I don't see any wrong with the using directive in itself.
    Last edited by Elysia; 04-20-2008 at 10:30 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.

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by laserlight View Post
    EDIT:
    This may be much ado about nothing: Geany 0.11's C++ source template does not come with a using directive.
    Great! They fixed the "bug".

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Right, and by putting "using namespace x;" at the top of all your source files, you've essentially put x's names right back into the global namespace.
    Almost, since one can still fully qualify names.

    Putting them at function scope, however, introduces less risk.
    I do not think it introduces less risk (you still do not have control over what names might be introduced), but rather it makes it easier to diagnose a name collision.

    using declarations (like "using std::string") is even less risky.
    To avoid Sutter and Alexandrescu's counterexample, I think this must only be done within a local, not namespace, scope; but having a bunch of using declarations at the start of a function body can be as tiresome as fully qualifying the names, so I tend to use them only when the name is used many times, or simply use a local using directive.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by laserlight View Post
    Almost, since one can still fully qualify names.
    But then there's no point in "using namespace ...".

    I do not think it introduces less risk (you still do not have control over what names might be introduced), but rather it makes it easier to diagnose a name collision.
    If you have 20 member functions defined in a .cpp file and put 'using namespace x;' at the top of the file, all 20 functions have the potential for name collisions. If you only need it in one and limit the directive to that function, it lessens the risk.

    To avoid Sutter and Alexandrescu's counterexample, I think this must only be done within a local, not namespace, scope; but having a bunch of using declarations at the start of a function body can be as tiresome as fully qualifying the names, so I tend to use them only when the name is used many times, or simply use a local using directive.
    I agree. I actually prefer fully qualifiying std::string or std::vector anyway, unless like you say, they are used a lot.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21