Prev: Testing Framework for MFC
Next: Crash in CFileDailog when called from hook procedure (OFNHookProc)
From: RB on 9 May 2010 20:19 > **** > I fail to see how qualifying a namespace can "bloat" an application. I am not sure what > you read that suggests this, but a namespace is merely syntactic directive to the > compiler, and generates no code. > **** >>But putting it in a cpp file >>only affects that cpp file of the app. >>What exactly does this mean. > *** > As I said, I have no idea what this could possibly mean, since the notion of a syntactic > directive "bloating" an application seems inherently nonsensical. > **** Hey thanks for the reply! I got that at http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/A-Beginners-Tutorial-For-stdvector-Part-1.htm about one page or so down it says "This is okay for small projects, as long as you write the using directive in your cpp file. Never write a using directive into a header file! This would bloat the entire namespace std into each and every cpp file that includes that header. For larger projects, it is better to explicitly qualify every name accordingly. I am not a fan of such shortcuts. In this article, I will qualify each name accordingly. I will introduce some typedefs in the examples where appropriate-for better readability." -- I saved the rest of your reply on namespace and includes to my notes folder. Later........RB
From: Doug Harrison [MVP] on 10 May 2010 01:33 On Sun, 9 May 2010 20:19:26 -0400, "RB" <NoMail(a)NoSpam> wrote: >Hey thanks for the reply! I got that at >http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/A-Beginners-Tutorial-For-stdvector-Part-1.htm >about one page or so down it says >"This is okay for small projects, as long as you write the using directive in your cpp file. Never write a using directive into a >header file! This would bloat the entire namespace std into each and every cpp file that includes that header. For larger projects, >it is better to explicitly qualify every name accordingly. I am not a fan of such shortcuts. In this article, I will qualify each >name accordingly. I will introduce some typedefs in the examples where appropriate-for better readability." The "bloating" they're talking about brings all the type, variable, function, and other scoped names declared by the namespace into the scope of the using-directive. This can lead to ambiguity and naming conflicts. See the FAQ for more on why it's bad: http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5 -- Doug Harrison Visual C++ MVP
From: Joseph M. Newcomer on 10 May 2010 12:59 This is a completely silly use of the word "bloat", and all it means is that all of the std:: namespace is visible without qualification (see the search rules discussion I gave earlier!) in every .cpp file. It is not clear why this is necessarily a Bad Thing, but since I do not believe in the 'using namespace' construct, I find the discussion sort of pointless. It does not change code size. It might affect compilation speed because each unqualified name now must be searched for in the std:: namespace, but frankly, I think you should avoid the use of 'using namespace std' entirely. I find it, like the author you quote, a pointless shortcut. joe On Sun, 9 May 2010 20:19:26 -0400, "RB" <NoMail(a)NoSpam> wrote: > > >> **** >> I fail to see how qualifying a namespace can "bloat" an application. I am not sure what >> you read that suggests this, but a namespace is merely syntactic directive to the >> compiler, and generates no code. >> **** >>>But putting it in a cpp file >>>only affects that cpp file of the app. >>>What exactly does this mean. >> *** >> As I said, I have no idea what this could possibly mean, since the notion of a syntactic >> directive "bloating" an application seems inherently nonsensical. >> **** >Hey thanks for the reply! I got that at >http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/A-Beginners-Tutorial-For-stdvector-Part-1.htm >about one page or so down it says >"This is okay for small projects, as long as you write the using directive in your cpp file. Never write a using directive into a >header file! This would bloat the entire namespace std into each and every cpp file that includes that header. For larger projects, >it is better to explicitly qualify every name accordingly. I am not a fan of such shortcuts. In this article, I will qualify each >name accordingly. I will introduce some typedefs in the examples where appropriate-for better readability." Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: RB on 10 May 2010 14:21 Ok I understand that ok. So if I don't put the using namespace::std I must always prepend the std:: as in std::vector<int> array(size); However in my current project if I take out the using namespace std:: I still get an error even with the std:: prepened on one of them. I have above the class implementation in my view class #include <vector> #include <iostream> and then in the button handler implementation I have size_t size = 10; std::vector<int> array(size); for (size_t i = 0; i < size; i++) { array[i] = i; // this goes ok with no error } for (size_t j = 0; j < size; j++) { cout << array[j] << endl; //error C2065: 'cout' : undeclared identifier } std::vector<int> v(2); // these last 3 also compile ok int& first = v.front(); int& last = v.back(); //// I get the one error shown on the line above. But I can put back the using namespace std; and also take out the prepended std:: of the two vectors and it then compiles with no errors and no warnings.
From: RB on 10 May 2010 14:24
Thanks for the reply I am learning much from all the input. I am trying hard to digest and learn from all angles of this stuff. |