2

The following code will cause compile errors in g++4.4:

// File test.cpp
namespace A
{
    #include <iostream>
}

int main()
{
    return 0;
}

I have this requirement because some third party library comes without namespace protected, and if I directly include these headers, my namespace is polluted.

As a result, I tried to create namespaces for those libraries, but if the library includes some "std headers" the above approach will fail.

4
  • Are you trying to rename the namespace of a STL (std) header, or to move what's in a 3rd party header into a namespace? The subject and example code suggest the former but the description suggests the latter. Commented Jan 5, 2011 at 7:35
  • @Leo Davidson:I want to move what's in a 3rd party header into a namespace Commented Jan 5, 2011 at 7:44
  • @Leo Davidson: It seems he wants to put whats in a 3rd party header into a namespace, but can't because that header includes standard lib headers. I don't think there's much you can do, short of editing the header yourself. Commented Jan 5, 2011 at 7:55
  • 1
    If it's a small API (or one you do not use very much of) you could create a thin wrapper around it which forwards calls to the API via functions that are in a namespace. (The wrapper's .cpp file would have to pollute its own global namespace to include the library's header, but that's all; anything including the wrapper's .h file would be pollution free.) This would be a big hassle if it's a large API, of course. Commented Jan 5, 2011 at 8:03

3 Answers 3

8

I believe 17.4.2.1 [lib.using.headers] forbids including standard library headers in a namespace :

A translation unit shall include a header only outside of any external declaration or definition, and shall include the header lexically before the first reference to any of the entities it declares or first defines in that translation unit.

I don't think there is anything you can do besides filing a request to the library author.

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

Comments

1

This approach will most likely lead you to trouble. You could approach the problem by manually including each such standard header before entering the namespace, and the include guards would take care of not re-including the header inside the namespace.

That would take care of your current error, but it would on the other hand break too many other things --if the library is precompiled then the symbols used in your code and the symbols in the binary library would be different symbols (libname::foo() used in your code, ::foo() defined in the binary). Even if the library is header only, any fully qualified access to the library within the library itself would break (void foo() { ::bar(); } where foo and bar are inside the library).

A valid approach that you might want to try (even if cumbersome, and requiring real work) would be writting a wrapper that is within it's own namespace, and uses the library. Then include your wrapper instead of the actual library headers.

My advice, on the other hand, would be ignoring the problem altogether. Declare your own objects within your namespaces and that would take care of the possible name collisions. As long as you keep away from using namespace statements you will be fine.

Comments

0

Use fully qualified names when using calls to standard libraries like std::cout instead of writing using namespace std;. This way, both can coexist.

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.