Thread: What's "best practice" between 'using namespace std' and 'std::count/std::cin'

  1. #16
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Tbh, anything that does I/O is immediately non-performant, I've found.

  2. #17
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    using namespace and even 'using ...' should probably be avoided in header files, even if encapsulated in a namespace (i.e. not in the global namespace). At first it is easy to think that something like the following is okay:

    Code:
    //header1.h
    /* standard includes */
    namespace my_awesome_project {
    using namespace std; // OK?
    
    /* lots of code */
    
    }
    
    //header2.h
    /* standard includes */
    namespace my_awesome_project {
    using namespace std; // OK?
    
    /* some more lots of code */
    
    }
    Users of the code in these headers can make use of it easily enough by either using a 'using namespace' or by using a namespace alias:

    Code:
    //user1.cpp
    /* standard includes */
    #include "header1.h"
    using namespace std;
    namespace awe = my_awesome_project;
    using awe::Some_type;
    
    int user1() {
      Some_type obj(123);
      cout << awe::some_function(obj) << endl;
      return 0;
    }
    
    //user2.cpp
    /* standard includes */
    #include "header2.h"
    namespace proj = my_awesome_project;
    using std::endl;
    
    int user2() {
      std::vector<proj::Some_other_type> vt;
      vt.push_back(proj::make_something_else(123));
      vt.push_back(proj::make_something_else(124));
      vt.push_back(proj::make_something_else(125));
      std::cout << proj::some_other_function(vt) << endl;
      return 0;
    }
    Notice that user2 has decided to avoid 'using namespace std' and use std:: everywhere. That's fine, but suppose he changes the vector declaration to the following:

    Code:
    proj::vector<proj::Some_other_type> vt;
    proj::vector is actually std::vector, but because you imported std into my_awesome_project (which user2 has renamed to proj), you have unintentionally given him access to std::vector through your namespace. The author of my_awesome_project almost certainly intented that only her own types and functions should be in the namespace.

    Additionally, if a user does end up referring to something like proj::vector, the maintainer of my_awesome_project has now committed herself to forever include the 'using namespace' lines in her header files, as not to do so would break user2's code.
    Last edited by c99tutorial; 09-29-2016 at 04:58 PM.

  3. #18
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Mutant - sure, but there's a point to be made that something can be slow, and then it can be ten times slower...

    @c99tutorial - good example.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I/O can just choose to flush whatever you input to it. Some implementations can also choose to flush if they see a newline. There's not much you can do to avoid flush, but you can guarantee that it will flush by using std::endl.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Yes, and guaranteeing that it flushes, say, every iteration of a loop, or even multiple times per iteration, may degrade the performance, which is easily testable. That's true if the implementation doesn't flush on every input or on every newline in said input. Of course, even if it does flush every time - we can just choose to buffer on our own.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-12-2015, 10:28 AM
  2. Question on "Jumping into C++" Chapter 13 practice problem 1
    By Ben Sturm in forum C++ Programming
    Replies: 9
    Last Post: 04-01-2015, 01:16 PM
  3. Replies: 4
    Last Post: 08-24-2014, 08:26 PM
  4. "Jumping into C++" Chapter 5 Practice Problem
    By Grae in forum C++ Programming
    Replies: 4
    Last Post: 09-04-2013, 01:46 PM
  5. Replies: 18
    Last Post: 03-07-2013, 06:55 AM

Tags for this Thread