I was reading Accelerated c++ chapter 4 where they teach about dividing a c++ program in different files. Here, they write that we should not use the "using _::" construct in header files because whoever is including the header might want to use a different implementation. But while implementing the methods in the header file, the use of "using" is fine. Can you please clarify this? While linking the implementation object file, won't the program eventually use the "using::" construct? Here is the code:
//median.h file
#ifndef GUARD_median_h
#define GUARD_median_h
#include <algorithm>
#include <vector>
double median(std::vector<double>); // <<<<<<<< no "using std::vector"
#endif
But in median.cpp:
#include <vector>
#include <stdexcept>
using std::vector; // <<<<< "using" construct used
using std::domain_error; // <<<<< "using" construct used
double median(vector<double> vec){
if(vec.size() == 0) throw domain_error("median for an empty vector not defined");
//....... rest of the implementation
}
To clarify a bit more:
Here is my client calling the above header:
#include "median.h"
using my_vector_impl::vector;
//..some function...
std::vector v1;
double med = median(v1);
Am I right in saying that we prevent "using std::vector" in header so that we can use line 2 in above code?