Thread: C ++ Standards

  1. #1
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69

    C ++ Standards

    Can anyone exaplin or give me a link to an explanation fo the laetst c++ standards?

    I gather std::cout<< should be used rather than just cout<< for example.

    What other changes are there? and what does the std:: part mean?

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    std:: introduces the namespace from which you are selecting a name, thus

    cout << ...

    assumes that the namespace is already visible to the process at that point, you've used a "using namespace std;" for example,

    std::cout << ....

    specifically says use the "std" namespace now, for this name resolution.

    Neither is more standard then the other, as both are part of the ANSI standard. What you will find is from a stylistic point of view std::cout tends to be favoured over a using clause.

    That said, if you are using a lot of items from the a specific namespace, use a using, the resolution operators simply clutter the code.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    So arwe you saying if i declare

    Code:
    using namspace std;
    then i can jsut type cout<<, I thought i still had to type std::cout<<


    sry im a bit confused

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > then i can jsut type cout<<,
    Well if you go for the brute-force using namespace std;
    That just gets everything from the standard name space

    Imagine you have a large project where two different things define cout
    using namespace std;
    using namespace foo;

    Then you might do
    std::cout << "hello";
    foo::cout << "world";

    But you could no longer do
    cout << "woe is me";


    For small programs which only use the standard namespace, its probably OK
    For large programs with many namespaces, you'll definitely want to be more specific about your intentions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    ok, so can you tell me what namspace does? whats the difference between std and foo?


    The thing is I havent been taught this by my teacher so im trying to work it out myself....

    (you might remmeber i was the guy who was taught by his teacher to use void main() (which btw i dont any more ) and also was using an ancient Borland compiler (ive now chanegd to dev-c++)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    std is well, standard - everybody gets a std namespace

    foo is made up - you can define a foo namespace and you can call it whatever you want
    explosive::finder;
    There's an example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I think of namespaces as just another identifier. Say I had two friends named Smith. One lived Chicago, the other in Paris. If I said I was going to visit my friend Smith, you wouldn't know which place I was going. But if I said I was going to visit my friend Smith in Paris, you'd know. The city in which my friends live is equivalent to a namespace in that it allows you to differentiate between two different things with the same name. The default namespace is std, but you can declare a namespace at any time you want and give it any name you want. Then you just have to tell the compiler which version of the item you want to use by indicating the namespace in which it resides.

  8. #8
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    ok thank you for clearing that up for me

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    If you don't like typing but still don't want to clutter the global namespace, the following thing is legal:
    Code:
    #include <iostream>
     
    int main()
    {
      using namespace std;
      cout << "Hej ! ";
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question - linux - gcc - c standards
    By AMAMH in forum C Programming
    Replies: 12
    Last Post: 12-03-2009, 02:49 AM
  2. C standards question
    By jim mcnamara in forum C Programming
    Replies: 7
    Last Post: 09-14-2007, 02:59 PM
  3. Standards
    By Brain Cell in forum C Programming
    Replies: 23
    Last Post: 07-01-2004, 06:22 AM
  4. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM
  5. Keeping up with newest standards
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2002, 08:06 AM