What is Proper?

This is a discussion on What is Proper? within the C++ Programming forums, part of the General Programming Boards category; The way I was taught C++ looks like this... Code: #include <iostream> using namespace std; int main() { cout<<"Hello World!"<<endl; ...

  1. #1
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50

    What is Proper?

    The way I was taught C++ looks like this...

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    cout<<"Hello World!"<<endl;
    
    //or you could use this to start a new line...
    
    cout<<"Hello World!\n";
    
    system("pause");
    
    }//end main
    But after reading the first couple or chapters in a book I have purchased, I noticed that their method is slightly different...
    i.e.

    Code:
    #include <iostream>
    
    int main()
    {
    
    std::cout << "Hello World\n";
    
    return 0;
    
    }//end main
    What I'm wondering is, is it bad habit to use 'using namespace std;' in place of always typing out that extra 'std::'?

    I find that it's faster, but I really want to learn the right way. Also, are the spaces between std::cout, the insertion operator (<<) and the text ("Hello World") important? Or are the optional? I can see them as being a way to make the code more reader friendly, but I don't understand why else they would be needed.

    Thanks for the help...

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    using namespace std; and std::cout are both proper ways to do it. The std::cout way is faster because it is not loading ALL the std commands into memory(which is what using namespace std does). The std::cout way is directly accessing only that command so it is recommended by some. The spaces in between the cout and << operator are optional. They are used because it looks nicer and is easier to read. Other than that, you don't NEED them.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    Excellent, that is exactly what I was wondering.

    I suppose instead of 'using namespace std;', just stating what I'm using would be faster...

    Such as...
    Code:
    using std::cout;
    using std::endl;
    One last thing, I noticed that somewhere on this board someone said (sorry for the obscurity, but I can't remember where I saw this :P) that stating what std functions I'm using globally was bad practice... should I state them in each function instead of globally?
    Which is faster?

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Personally, if you need more then a few of the std commands I just use the entire namespace. I feel that it makes the code more readable and the speed difference is not HUGE, is it?

  5. #5
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    Quote Originally Posted by jmd15
    using namespace std; and std::cout are both proper ways to do it. The std::cout way is faster because it is not loading ALL the std commands into memory(which is what using namespace std does). The std::cout way is directly accessing only that command so it is recommended by some.

    well, i dn't think the others will agree with that.

    From what i have learned, "using namespace;" defeats the purpose of using namespaces. Hence i'll say that "using namespace;" is NOT really the proper way to do it!

    maybe the other more qualified people here can clarify on this.


    (check the FAQ for infos about namespaces! There is a good explanation about it there)

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Flakster
    One last thing, I noticed that somewhere on this board someone said (sorry for the obscurity, but I can't remember where I saw this :P) that stating what std functions I'm using globally was bad practice... should I state them in each function instead of globally?
    Which is faster?
    This has nothing to do with speed. The compiler will in both ways generate the same code. Namespaces do not exist in the compiled code. They are just a tool that can help you write safer code. If you pull in a namespace at global scope it's just the same as saying "I don't need that feature, I know what I'm doing".
    It's your joice.
    Kurt

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    I like using std::whatever because the more complex code I write, the easier it is to scan my code and know what references are being made to namespaces, classes, whatever.

  8. #8
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Quote Originally Posted by wakish
    well, i dn't think the others will agree with that.
    Then let them disagree!
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  9. #9
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I agree with Zuk, its nothing to do with speed, but for security (as a lot of C++ features are, const for example). If you declare using namespace std; you are saying that you KNOW you dont have another cout/cin/endl/etc function name somewhere else in your program, because if you did happen to have your own defined cout, it would result in funky problems. Thats what I meant by global.. I was probably the persons post you read, Flakster.

    Another purpose is simple so you know where things are coming from, of course that isnt necessary for cout/cin.. I'm sure every C++ programmer knows where those come from

    It's probably good coding practice to use std:: on every cout/cin just because it helps you remember when you start using your own namespaces to do that... Plus it makes your code look more complex, which is cool

    P.S. Spaces/new lines are not necessary in many places, but are just for readability.

    Code:
    std::cout<<"blah"<<std::endl;
    cout << "hmm"<< endl;
    
    int aFunction(int param) {
    int aFunction ( int param ) {
    int aFunction (                                             int param) {
    
    if(something) {
    if(something){
    if ( something ){
    if(something)
    {
    }
    Last edited by Dae; 10-03-2005 at 05:00 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The general rule of thumb is if the file is a header file always specify explicitly. you dont want a using directive in a header file. On the other hand in a source file, if its small and you know that including the whole of the std namespace will not cause nameclashes then using namespace std is perfectly acceptable. When the source file gets more complicated and uses a couple of libraries then the potential for nameclashes is much greater and it becomes less of a good idea to use a using namespace std and instead either use a using declaration like using std::cout or explicitly state as you did in the header files.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    as a general rule, you should avoid
    using namespace blah
    directives in header files, but they're not too bad in individual .cpp files. As other peple have mentioned, it makes zero difference to the compiled code.

    As for spaces, it's mostly a matter of style. Personnally I like a bit of whitespace, it costs nothing and

    cout << "you have " << numApples << " apples;

    is more readable then

    cout<<"you have "<<numApples<<" apples;

    The one big C++ gotcha with white space is when you have a template of templates, e.g.

    list<vector<int>/*need a space here*/> listOfIntVectors;

    You HAVE to have a space between the '>' characters or C++ will interpret it as a '>>' operator;

  12. #12
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    This has helped alot guys, thanks.

  13. #13
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    "using namespace ...;" does not defeat the purpose of using namespaces. You can use a whole namespace and then if you have another namespace that has a function or class with the same name, just qualify it with that namespace when you declare your variables. It's really not a big deal in the real world. The best advice I have, if you are getting a job, is to pick up on the standards of your co-workers. There is nothing more annoying than the new guy coding things in different formats than everyone else. You all need to write your code similarly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proper nouns
    By Ducky in forum C++ Programming
    Replies: 12
    Last Post: 12-31-2007, 10:11 AM
  2. Making proper wrappers.
    By bladerunner627 in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2005, 04:18 PM
  3. Finding a 'Proper Subset'
    By SlayerBlade in forum C Programming
    Replies: 1
    Last Post: 09-28-2005, 05:16 AM
  4. Proper way to read in a file
    By elaechelin in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2004, 10:00 AM
  5. How to find the proper divisor?
    By yusiye in forum C Programming
    Replies: 6
    Last Post: 07-24-2002, 01:14 PM

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