Soroush Rabiei
Soroush Rabiei

Reputation: 10868

namespace inclusion in header and template classes

Well, this is not a real programming issue, but more about good/bad practice

I'm working of my final project of CSP course which is a library with several solvers. Most classes are heavily templated. AFAIK there is no elegant way to separate source and header to get a clean binary and development staff. Actually there is no real binary here. Code generation stage will be done by compiler when within final code.

On the other hand, I'm using boost asio library to do networking staff in a portable manner. From basic programming course, I know it's known as a bad practice to use using namespace in headers. Because doing so will limit final user to use a more limited naming options within his/her private code, and is more conflict prone.

Finally, to make code clear I would like to unisg namespcae or using in my code. My question is what is best way to do so? Am I doing this wrong? Currently I'm using usings inside functions in header (not at global scope) like this:

// File : abt-solver.h
template<typename valueType>
inline void AIT::ABT_Solver<valueType>::connect() {
    using boost::asio::ip::tcp;    // <===== Here
    using namespace std;           // <===== And here
    tcp::resolver::query query(this->address, this->portNumber);
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
    tcp::resolver::iterator end;
    boost::system::error_code error = boost::asio::error::host_not_found;
    while (error && endpoint_iterator != end) {
    // blah blah blah ...

Upvotes: 0

Views: 86

Answers (2)

vlad_tepesch
vlad_tepesch

Reputation: 6891

since using the using inside a limited scope does not affect the parent namespace or or namespace of the including file i do not see any problems.

regarding separation of source from declarations: boost uses the following scheme: the declarations reside in a *.hpp file and the implementations are in another file (i could not recall the extension atm).

at the end of the hpp htey include the file with the definitions. Of course you have to remember that every thing that is declared in the implmentation file is also visible to the header. so still no using is allowed

Upvotes: 1

Jonathan Wakely
Jonathan Wakely

Reputation: 171263

Yes, it's fine to use using at function scope, it doesn't affect anything outside that function.

Upvotes: 3

Related Questions