|
Prev: doubt on non-type template-parameter
Next: Templates: default template argument in a friend class template declaration
From: -Rick- on 30 Mar 2008 00:41 #include <iostream> using namespace std; int main(){ cout<<"Hello world"<<endl; return 0; } OR #include <iostream> int main(){ std::cout<<"Hello world"<<std::endl; return 0; } I've been told many times that the second option is better coding practice, but I fail to understand why. I mean, if one had to move to another namespace in the program, then only then do you have to change. The first option, imo, makes the code more readable. What is this forum's opinion? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Sashi Asokarajan on 30 Mar 2008 04:47 -Rick- wrote: > change. The first option, imo, makes the code more readable. > What is this forum's opinion? it depends on the project(s) your'e working on. mostly they have their own style guide. and it's a good advice to accept it. heres is an example of the FreeBSDs kernel style guide: http://www.freebsd.org/cgi/man.cgi?query=style I *personally* prefer: #include <iostream> int main() { using std::cout; using std::endl; cout << "Hello world" << endl; return 0; } import cout and endl from std:: into current namespace. I also tabstop only 1 space per block. so at the end, I can easily pickup the scope depth. [...] } // inner 2 } // inner 1 } // outer -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: kiran on 30 Mar 2008 04:43 On Mar 30, 11:41 am, -Rick- <rick.sof...(a)gmail.com> wrote: > I've been told many times that the second option is better coding > practice, but I fail to understand why. I mean, if one had to move to > another namespace in the program, then only then do you have to > change. The first option, imo, makes the code more readable. > What is this forum's opinion? Either of the options should be fine. However, it depends on what kind convention is followed at any work place. I usually keep scope resolution with std in .h files and use namespace declartion in cc files. Regards, kiran -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel T. on 30 Mar 2008 04:47 -Rick- <rick.softly(a)gmail.com> wrote: > #include <iostream> > using namespace std; > int main(){ > cout<<"Hello world"<<endl; > return 0; > } > > > OR > > #include <iostream> > int main(){ > std::cout<<"Hello world"<<std::endl; > return 0; > } > > I've been told many times that the second option is better coding > practice, but I fail to understand why. I mean, if one had to move to > another namespace in the program, then only then do you have to > change. The first option, imo, makes the code more readable. > What is this forum's opinion? Without namespaces, library vendors who don't want their identifiers to conflict with other libraries, have to prepend every identifier they define with some sort of "wart". One often sees these these warts in C code for example. If one refuses to put using declarations and definitions in their code (as in your second example,) then it is effectively the same as C's system of having warts prepending every identifier. This is a kind of hungarian notation. The namespace feature of C++, along with careful use of using declarations and definitions, reduces the need for these warts. IMHO, that is the primary use of the namespace feature, making code cleaner by reducing the hungarian warts needed throughout the code base. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mateusz Adamczyk on 30 Mar 2008 04:49 On 30 Mar, 17:41, -Rick- <rick.sof...(a)gmail.com> wrote: > #include <iostream> > using namespace std; > int main(){ > cout<<"Hello world"<<endl; > return 0; > > } > > OR > > #include <iostream> > int main(){ > std::cout<<"Hello world"<<std::endl; > return 0; > > } > > I've been told many times that the second option is better coding > practice, but I fail to understand why. I mean, if one had to move to > another namespace in the program, then only then do you have to > change. The first option, imo, makes the code more readable. > What is this forum's opinion? > Read the FAQ(http://www.parashift.com/c++-faq-lite/coding- standards.html). IMHO, the main use for namespaces is to avoid name conflicts. So, in first option You can get strange errors, when Your created something with name exact the same as in STL. Maybe cout is not a good example, but You can have function count, which counts something. Then, You can probably get strange errors, if You use count from std in the same code. Writing std::count is maybe longer, but it can save You many days of searching strange error. What's more, it shows of which 'count' You use - so it can really be more readable. Regards, Mateusz Adamczyk -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Next
|
Last
Pages: 1 2 3 4 5 6 7 Prev: doubt on non-type template-parameter Next: Templates: default template argument in a friend class template declaration |