Search code examples
c++coding-stylecode-readability

Should `using std::cin` and `using std::cout` be avoided or encouraged?


I searched this site and people says you should avoid using using namespace std. I totally agree. However, what about using std::cin and using std::string? Should this be avoided or encouraged?

I know always type std::cin is the safest choice, but it is very tedious to type them again and again.

However, when you type using std::cin etc in the begining of the file, it seems very crowd. For example, this simple program read and calculate student grade, in front of it, there are too many using std::, it look very uncomfortable.

#include <iostream>
#include <ios>
#include <iomanip>
#include <stdexcept>
#include <vector>
using std::cin;             using std::cout;
using std::istream;         using std::vector;
using std::setprecision;    using std::domain_error;
using std::string;          using std::getline;
using std::streamsize;

istream& read_hw(istream& in, vector<double>& homework);
double grade(double mid_exam, double final_exam, \
        const vector<double>& homework);

int main()  {

    std::string name;
    std::getline(std::cin, name);
    std::cout << "Hello, " + name + "!" << std::endl;

    double mid_exam, final_exam;
    std::cin >> mid_exam >> final_exam;

    std::vector<double> homework;
    read_hw(std::cin, homework);

    try {
        double final_grade = grade(mid_exam, final_exam, homework);
        std::streamsize prec = std::cout.precision();
        std::cout << "your final grade is:" << std::setprecision(3)
            << final_grade << std::setprecision(prec) << std::endl;
    }
    catch(std::domain_error)    {
        std::cout << std::endl << "No homework entered!" << std::endl;
        return 1;
    }

    return 0;
}

std::istream& read_hw(std::istream& in, std::vector<double>& homework)   {
    if(in)  {
        homework.clear();
        double x;
        while(in >> x)  {
            homework.push_back(x);
        }
    }
    in.clear();

    return in;
}

double grade(double mid_exam, double final_exam, \
        const std::vector<double>& homework)    {
    std::vector<double>::size_type i, size;
    size = homework.size();
    if(size ==0)    {
        throw std::domain_error("no homework grade entered!");
    }
    double sum = 0;
    double average = 0;
    for(i = 0; i < size; ++i)   {
        sum += homework[i];
    }
    average = sum/size;

    return mid_exam*0.3 + final_exam*0.3 + average*0.4;
}

In python's tutorial, it says:

Remember, there is nothing wrong with using from Package import specific_submodule! In fact, this is the recommended notation unless the importing module needs to use submodules with the same name from different packages.

I want to know what I should do in c++ programs.


Solution

  • Never use using namespace std; or similar in a header file as it can cause all sorts of ambiguity issues that arise due to namespace pollution. If you obey that rule then folk who have to include your headers will thank you for it. I'd also avoid any sort of using std::... in headers for similar reasons. Learn to love the more verbose std:: notation.

    What you get up to in a source file is largely down to you. Any recommendations here are largely opinion-based, but personally, I never use using just to save typing, but rather when it's unavoidable such as bringing shadowed functions back into a namespace, and in template metaprogramming.