About syntax and std::everything

This is a discussion on About syntax and std::everything within the C++ Programming forums, part of the General Programming Boards category; Ok so I got a question. Why do a bunch of the very strict advocates of standard everything (among which ...

  1. #1
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113

    About syntax and std::everything

    Ok so I got a question. Why do a bunch of the very strict advocates of standard everything (among which I agree, by the way - standard makes things easier) always put std:: in front of everything??

    I mean, isn't
    Code:
    inline void wait_for_keypress()
    {
      std::cin.ignore(std::cin.rdbuf->in_avail() + 1);
    }
    rather more needlessly complicated
    than say,
    Code:
    using namespace std;
    inline void wait_for_keypress()
    {
      cin.ignore(cin.rdbuf->in_avail() + 1);
    }
    or so?
    Is there actually a reason for using the prefix std:: all the time rather than 'using namespace std'? Actually namespaces is one of the things I haven't really gotten into yet, but I know using namespace std is supposed to be part of the standard way of doing things, and I know that wether it's standard or not, prefixing everything with std:: is a royal pain.
    (plus I never put std:: in front of things, and they still work)

    Any info on this you can point me towards?
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there actually a reason for using the prefix std:: all the time rather than 'using namespace std'?
    The using directive opens every name in the namespace to visibility. This is generally considered a bad thing and defeats the purpose of namespaces. Using std:: is far more strict in what names are visible and where...at the cost of programmer convenience. Though a nice happy middle ground is a series of using declarations:
    Code:
    // Make visible only cout, cin, and endl
    using std::cout;
    using std::cin;
    using std::endl;
    
    ...
    
    cin>> something;
    cout<< something <<endl;
    My best code is written with the delete key.

  3. #3
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Well, it all still seems a little pointless to me (after all, if it's standard, why have it locked inside a namepsace wherein you have to unlock each piece before you can use it?) but I suppose I'll understand it eventually (as I have with alot of other things)
    Thanks for the swift reply.

    --edit--
    luv your avatar btw
    -/edit--
    Last edited by Cobras2; 06-04-2003 at 01:49 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >why have it locked inside a namepsace wherein you have to unlock each piece before you can use it?
    Have you ever written a program using a third party library (or two different libraries) only to have name collisions make your job ten times harder? That's what namespaces are designed to avoid. They're virtually useless and annoying for the trivial programs you see on these forums, but when the application gets larger, they become invaluable.
    My best code is written with the delete key.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    >>Well, it all still seems a little pointless to me (after all, if it's standard, why have it locked inside a namepsace wherein you have to unlock each piece before you can use it?)

    It stops clashes.......in a library like the std library there might be 1 component that shares a name with another component in another library that you may want to use.......so without the namespace qualifiers, the compiler will be unable to decide which part of what library you want to use, that will stop it compiling.....

    That's why you should avoid using namespace std unless you are coding a very small app or say a demonstration of something

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "Well, it all still seems a little pointless to me (after all, if it's standard, why have it locked inside a namepsace wherein you have to unlock each piece before you can use it?) but I suppose I'll understand it eventually"

    Namespaces are used to prevent name clashes. If you have 100 programmers on a project all writing lots of code, the chances are they are going to have some variables with the same names, which could cause the program to behave unexpectedly and create debugging headaches. If they enclose their code in unique namespaces, then their variables are not the same.

    Personally, I always use:

    using namespace std;

    in my code, because it's a pain otherwise. Since I haven't done anything very complex and I don't plan on naming any of my variables "cout" or "cin", I don't worry about it.
    Last edited by 7stud; 06-04-2003 at 01:30 PM.

  7. #7
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Is there actually a reason for using the prefix std...
    Yes there is. Namespaces were introduced to avoid name clashes (I think the term is Global Pollution). It may seem useless for now but when you start using other libraries than Standard c++ offers problems might occur. E.i. What would happen if you were using an algorithm library (without namespaces) and the older c++ header files (no namespace) the sort function which is defined in both libraries. The complier woudlnt know which sort function to use, becuase there are both in the global namespace. But if they are in different namespaces this problem wouldnīt be a problem.

    Namespaces doesnīt solve the Global Pollution to 100% but itīs a step in the right direction.
    Last edited by ripper079; 06-04-2003 at 01:33 PM.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  8. #8
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    "...the only bad thing about it is potential name clashes and namespace pollution. Which is why I do my own thing and don't tell other people what to do in a matter of style."

    >This is generally considered a bad thing

    That sounds like you're trying to tell someone what to do when it comes to style. Why would anyone want to do a "bad thing"? (When it comes to programming, that is )

  9. #9
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Ok, I can see how that would be usefull - of course the only question remaining in my mind is, why anyone would be naming things in their own libraries the same as the standard functions.. (don't they use the standard functions too?) but I guess it's still quite possible for things like that to happen. I understand the general idea of namespaces - I just sort of wonder why the standard functions are in one, instead of everything *else* being in one.. but I suppose backwards compatibility has something to do with it..?
    Last edited by Cobras2; 06-04-2003 at 01:51 PM.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >why anyone would be naming things in their own libraries the same as the standard functions.. (don't they use the standard functions too?)
    If you've memorized every name used by the standard library you're either very experienced or have too much time on your hands.
    My best code is written with the delete key.

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A good example... the STL vector. Of course, it makes sense to name the vector a vector, after all, thats what it is. However, scientific programming libraries also often have classes named 'vector', and these too are logically named. Also, some software requires specialized versions of containers (lists, stacks, etc), and even strings.

    Granted, collisions with names like cout and cin are relatively rare, collisions with other names are not.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    As well as what Zack said....remember that the STL also has lots of alogoritms with common names

    reverse()
    rotate()
    search()
    count()
    copy()
    sort()
    etc...

    Easy to get a name clash with those!

  13. #13
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    rotate()? Sheesh, just when you thought you heard of them all...

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Sheesh, just when you thought you heard of them all...
    The standard C++ library consists of 51 headers, each contains its own macro, function, type, and object declarations. There are a lot of names to remember.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

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