Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"using" keyword in header implementation in C++

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?

like image 256
kmad Avatar asked Nov 12 '13 21:11

kmad


People also ask

Which keyword is used to declare a header file C?

Which of the following keyword is used to declare the header file? Explanation: The include keyword is used to include all the required things to execute the given code in the program.

How do you implement header files in C?

In C language, header files contain the set of predefined standard library functions. You request to use a header file in your program by including it with the C preprocessing directive “#include”. All the header file have a '. h' an extension.

What is using keyword in C?

In this article The using keyword has two major uses: The using statement defines a scope at the end of which an object will be disposed. The using directive creates an alias for a namespace or imports types defined in other namespaces.

Can I define function in header?

Unless you want the function to be inline , it is best to declare the function in the header and define it in a single source file and link it. If you declare the function as inline , then each of its function call in the source file will be replaced with the code inside the inline d function.


1 Answers

using is simply a compile-time shortcut to shorten names. It has no effect at runtime. Also, it only affects the source code which is at or below its own scope, so if you use it in the implementation file, no other files can see it, but if you use it in the header, all the files that include the header will have the using in them and their namespace will be polluted.

Edit:

To answer your updated question, your example isn't quite why you should avoid using in headers. This is why:

// Header:

using std::vector;

// Client:

#include <Header>

class vector { ... };

void f() {
    vector v; // Ambiguous because of something out of my control
}

This is the situation you want to avoid. You don't want to tell the people who use your libraries what names they can use, which you are doing when you do using.

like image 131
uk4321 Avatar answered Nov 14 '22 08:11

uk4321