- using namespace std - 8 Updates
- using namespace std - 1 Update
- problems and solutions : algorithms and data structures : interviews - 1 Update
arnuld <sunrise@invalid.address>: Jan 04 05:29AM I am reading Stroustrup's Tour of C++ where in section 3.3 (Namespaces) he encourages using /using namespace std/ whereas C++ FAQs say it is a bad ideas (for the reasons that I find quite logical) and we should use / std::/ all the time or just use /using st::cout/ etc. Pelsonally I think I will go with the FAQ but I was just disappointed that Stroustrup did not mention that how much bad idea it is to dump whole of the namespace into your program. He created C++, so whom should I give more preference, Tour of C++ of the FAQs ? or I did not understand what he is trying to say ? -- arnuld http://lispmachine.wordpress.com/ |
Paavo Helde <myfirstname@osa.pri.ee>: Jan 04 01:11AM -0600 arnuld <sunrise@invalid.address> wrote in > is to dump whole of the namespace into your program. > He created C++, so whom should I give more preference, Tour of C++ of > the FAQs ? or I did not understand what he is trying to say ? First I admit I haven't read the Stroustrup's text, just sharing my opinion about 'using namespace'. Like everything else in programming, some style decision is not the goal. The goal is to get something done, which in case of C++ often means writing software which is maintainable over long time. Software is maintainable if it is concsise, gives enough detail and does not involve too many distractions. The namespace prefixes are both the detail and the distraction, so one has to find some good balance when using them. IMO, the one-two namespaces which are used heavily in the code could be used without prefixes to reduce distraction and verbosity, and all other namespaces should be spelled out for providing the detail. In my code the heavily used namespaces are my own ones, so std:: is always spelled out, but I gather it may be different in some other code and 'using namespace std;' could be a good idea in some code (like in example code demonstrating the use of standard library). That said, 'using namespace' should never appear in a header file, to not force its decision about the balance upon other source code files. hth Paavo |
Marcel Mueller <news.5.maazl@spamgourmet.org>: Jan 04 09:13AM +0100 On 04.01.15 06.29, arnuld wrote: > I will go with the FAQ but I was just disappointed that Stroustrup did > not mention that how much bad idea it is to dump whole of the namespace > into your program. Well, the implementation of the namespace concept in C++ has advantages and disadvantages. Using them consequently avoids name clashes to a high degree. And fortunately the standard library has only a short namespace unlike Java or CLI. But it is still a code blow up and I hate this. Unfortunately the C++ header files are not very compatible to the using statement. The usings in headers 'infect' the entire program. Unlike Java they do not only apply to the current file. So you cannot reasonably use them in your headers. Practically, name clashes are not always that likely. If I have small projects I do not care about the rules, place using namespace standard into a common header and never think about namespaces again. But for larger projects this is no serious option. There you have to live with std:: at least in the headers. Marcel |
"Öö Tiib" <ootiib@hot.ee>: Jan 04 12:32PM -0800 On Sunday, January 4, 2015 7:30:12 AM UTC+2, arnuld wrote: > I am reading Stroustrup's Tour of C++ where in section 3.3 (Namespaces) > he encourages using /using namespace std/ That tour? https://isocpp.org/images/uploads/2-Tour-Basics.pdf It seems at 2.4.2 not 3.3 maybe it is different paper or different version of paper. > I will go with the FAQ but I was just disappointed that Stroustrup did > not mention that how much bad idea it is to dump whole of the namespace > into your program. It is a bad idea because namespace 'std' has lot of names in it. The name clashes will be quite likely. Especially when using in mix with the likes of 'boost' that often contains same names. The resulting error messages may be hard to understand. Clashes are less likely if your software (and libraries that you use) have major difference in naming. For example if you use only capitalized names. > He created C++, so whom should I give more preference, Tour of C++ of the > FAQs ? or I did not understand what he is trying to say ? It is important to mention what the line does in a tour because majority of standard library usage examples you find anywhere in the net use it. Also most of Stroustrup's own examples use it. |
JiiPee <no@notvalid.com>: Jan 04 08:35PM On 04/01/2015 08:13, Marcel Mueller wrote: > larger projects this is no serious option. There you have to live with > std:: at least in the headers. > Marcel so std:: in the headers and maybe using std::cout; int the cpp? |
Ian Collins <ian-news@hotmail.com>: Jan 05 09:41AM +1300 Yippee wrote: > so std:: in the headers and maybe > using std::cout; > int the cpp? That's a fair approach. -- Ian Collins |
Jorgen Grahn <grahn+nntp@snipabacken.se>: Jan 04 09:49PM On Sun, 2015-01-04, 嘱 Tiib wrote: > That tour? https://isocpp.org/images/uploads/2-Tour-Basics.pdf > It seems at 2.4.2 not 3.3 maybe it is different paper or different > version of paper. ... > It is important to mention what the line does in a tour because majority > of standard library usage examples you find anywhere in the net use it. > Also most of Stroustrup's own examples use it. To be more explicit: if this is the text arnuld read: The simplest way to access a name in another namespace is to qualify it with the namespace name [...] To gain access to all the names in the standard-library namespace, we can use a using-directive: using namespace std; Stroustrup just shows that you /can/ do that, not that you /should/. It's just a tour of the language: I think you're just supposed to learn there is something called namespaces, so you know there's such a tool in the language. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o . |
Mike Austin <spam@null.net>: Jan 04 01:55PM -0800 On 1/4/15 12:32 PM, Öö Tiib wrote: > It is important to mention what the line does in a tour because majority > of standard library usage examples you find anywhere in the net use it. > Also most of Stroustrup's own examples use it. Using boost more lately, I agree that importing whole namespaces is not a good idea. I do wish C++ has a more terse way of importing, such as: using std { cout, endl }; using boost { tuple, variant }; Currently, it looks more like this: using std::cout; using std::endl; using boost::tuple; using boost::variant; Another option is nesting namespaces: namespace iostream { using std::cout; using std::endl; } using iostream; Actually, I might just start using that :) Mike |
ram@zedat.fu-berlin.de (Stefan Ram): Jan 04 09:20PM >I am reading Stroustrup's Tour of C++ where in section 3.3 (Namespaces) >he encourages using /using namespace std/ whereas C++ FAQs say it is a >bad ideas (for the reasons that I find quite logical) and we should use / It depends on the grade of the code: When one is writing a small throwaway program where maintainability is not important, one can use it. For code that has a large expected lifetime and needs to be robust, one should not use it. I call that »library-grade code«. Bjarne Stroustrup does not use the »::std::« prefix in his book because these are just teaching examples and he deems them to be more readable this way. But nowhere does he strongly encourage the use of »using namespace std« for large library projects. |
iitk.cs@gmail.com: Jan 04 12:43AM -0800 Great book for algorithms and data structures based tough to crack interviews: Cracking Programming Interviews: 500 Questions With Solutions by Sergei Nakariakov http://www.amazon.in/dp/149545 http://www.amazon.com/Cracking-Programming-Interviews-Questions-Solutions/dp/1495459802/ |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment