10

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?

3 Answers 3

19

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.

Sign up to request clarification or add additional context in comments.

1 Comment

hi uk, could you answer the last part?
4

This simply means you can put using … in your C++ source. You could also reasonably put it in a private header that you include but no external user would include. You just don't want to mess with your client's environment and putting using in a header that they are required to include could do that.

While linking the implementation object file, won't the program eventually use the "using::" construct? The using keyword is just a convenience to the compiler, not something that changes what gets linked. It only affects the sources that use it.

Comments

2

The "using" declaration is only a compile time construct, saving you from having to type std:: (or another namespace) over and over again. In your own .cpp implementation file, it's fine to use a using declaration because you have control over that file. If you include it in a .h (header) file, every file that includes that header would also be including your using declaration, including the ones you don't have control over, and may never have even heard of.

A classic example would be an implementation file that's using tr1::shared_ptr, vs std::shared_ptr which only came along later. If your header file includes std::shared_ptr, then their code will no longer compile, and it's difficult for them to know why.

Incidentally, this is also why macros are now considered evil.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.