Use of "using" in header files - c++

I understood, that I should not use this in a header file:
using namespace foo;
Because it brings the namespace foo in the global scope for anyone who uses my header file.
Can I prevent this from happening, if I do it in my own namespace? For example like this:
namespace my_lib
{
using namespace foo;
// my stuff
// ...
}
Now the using namespace foo should be restricted to the scope of the namespace my_lib, right?

Yes. That is better than using using namespace foo at global level.
Yet better would be if you use foo::name syntax.
Now the using namespace foo should be restricted to the scope of the namespace my_lib, right?
Yes. It brings all the names from the namespace foo in the namespace my_lib which might cause name collisions in my_lib. That is why foo::name is the most preferrable approach.

Yes, if you do that then it just brings all the names from foo into my_lib -- which, as others have pointed out, may or may not be desirable.
One thing I would observe in addition to what others have said is that you can use the 'using directive within a namespace' idea as a way of simulating a using directive that is restricted to class scope. Note that this is illegal:
class C
{
using namespace boost; // for example
// ...
};
But you can do this instead:
namespace C_Namespace {
using namespace boost;
class C
{
};
}
using C_Namespace::C; // bring C itself back into the global namespace
Just thought you might find it useful if what you really want is to be able to define something (like a class) without writing a particular namespace prefix the whole time.

Related

How to block using a C++ namespace [duplicate]

One of the vagaries of my development system (Codegear C++Builder) is that some of the auto-generated headers insist on having...
using namespace xyzzy
...statements in them, which impact on my code when I least want or expect it.
Is there a way I can somehow cancel/override a previous "using" statement to avoid this.
Maybe...
unusing namespace xyzzy;
Nope. But there's a potential solution: if you enclose your include directive in a namespace of its own, like this...
namespace codegear {
#include "codegear_header.h"
} // namespace codegear
...then the effects of any using directives within that header are neutralized.
That might be problematic in some cases. That's why every C++ style guide strongly recommends not putting a "using namespace" directive in a header file.
No you can't unuse a namespace. The only thing you can do is putting the using namespace-statement a block to limit it's scope.
Example:
{
using namespace xyzzy;
} // stop using namespace xyzzy here
Maybe you can change the template which is used of your auto-generated headers.
You may be stuck using explicit namespaces on conflicts:
string x; // Doesn't work due to conflicting declarations
::string y; // use the class from the global namespace
std::string z; // use the string class from the std namespace
For future reference : since the XE version there is a new value that you can #define to avoid the dreaded using namespace System; int the include : DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE
How about using sed, perl or some other command-line tool as part of your build process to modify the generated headers after they are generated but before they are used?
Quick experiment with Visual Studio 2005 shows that you can enclose those headers in your own named namespace and then use what you need from this namespace (but don't use the whole namespace, as it will introduces the namespace you want to hide.
#include<iostream>
#include<stdio.h>
namespace namespace1 {
int t = 10;
}
namespace namespace2 {
int t = 20;
}
int main() {
using namespace namespace1;
printf("%d" , t);
printf("%d" , namespace2::t);
}

Using functions from namespace inside other namespace

Is it any way to omit outer namespace name for some functions from other namespace inside top-level one?
void sample_func();
namespace foo {
void first_func();
namespace bar {
void second_func();
void sample_func();
}
Everything is trivial for first_func(): just typing using foo::first_func; allows to call it just as fist_func();
Everything is simple if I want to call second_func without any prefix: just using foo::bar::second_func; allows to call it as second_func();
But is there any way to call it as bar::second_func();? It will increase code readability - much better to type and see something like bar::sample_func instead of full foo::bar::sample_func without names confusion: obviously using namespace foo::bar is not an option in that case.
UPD I am not interested in importing the whole foo or bar namespace (i. e. using namespace ... directive! I need just some functions from them.
You can use
namespace bar = foo::bar;
to import foo::bar into current namespace as just bar.
Prefix it with the namespace:: or :: if not in a namespace i.e
::sample_func();
foo::first_func();
bar::second_func();
bar::sample_func();
You can use
using namespace foo;
in any declarative region where you wish to use just first_func() and bar::sample_func().
Example:
int main()
{
using namespace foo;
first_func();
bar::sample_func();
}

using namespace: Convenience vs. namespace pollution

I'm writing a simple stopwatch template using <chrono>. Since these functions are in std::chrono, it would be convenient to write using namespace std::chrono to avoid bloating up my code lines. But since it's a template, the std::chrono would then be visible in all files which include the template.
What should I do in this situation? Do I have to pick one of the options, or is there a way to avoid the problems of both options?
You can use a namespace alias in you code:
namespace bc = boost::chrono;
and use
bc::duration d;
You can put this alias inside the functions you need boost::chrono so they do not contaminate other code.
template<T>
class foo {
void bar() {
namespace bc = boost::chrono;
bc::duration d;
}
// namespace alias bc is no longer in scope.
}
Alternatively, you can put the using namespace inside the scope of your template/function, however, I think this will reduce readability and will possibly cause conflicts between the boost and C++11 std namespaces.
template<T>
class foo {
void bar() {
using namespace boost::chrono;
duration d;
}
// using namespace boost::chrono is no longer in scope.
}

Using a namespace twice

In c++ Is it OK to include same namespace twice?
compiler wont give any error but still will it affect in anyway
Thanks,
EDIT:
I meant
using namespace std;
// . . STUFF
using namespace std;
It depends what you mean by 'include'. Saying:
using namespace std;
...
using namespace std:
is OK. But saying:
namespace X {
...
namespace X {
would create a nested namespace called X::X, which is probably not what you wanted.
This usage is fine, if it's what your talking about:
File: foo.h
namespace tools
{
class Widget
{
...
};
}
file: bar.h
namespace tools
{
class Gizmo
{
...
};
}
Twice in the same class/file? It shouldn't be a problem, but neither should it be necessary. I'd naively assume that you should be able to refactor your code to avoid the issue.
Are you asking whether following is okay ?
using namespace std;
using namespace std;
It is okay but normally I prefer to refer the namespace element with scope resolution.
ex:
std::vector
std::sort
I'm not entirely sure what you mean. You can put multiple classes in a single namespace (that's the whole idea). Each class generally has it's own files, so yes, you can use the same namespace in multiple files.
Though not technically required, it's a good idea to have a directory structure that represents the namespace hierarchy you create.
As for for the using directive: the compiler and/or intellisense most likely gives you a warning (the C# one does), but otherwise there is no effect.

How can I "unuse" a namespace?

One of the vagaries of my development system (Codegear C++Builder) is that some of the auto-generated headers insist on having...
using namespace xyzzy
...statements in them, which impact on my code when I least want or expect it.
Is there a way I can somehow cancel/override a previous "using" statement to avoid this.
Maybe...
unusing namespace xyzzy;
Nope. But there's a potential solution: if you enclose your include directive in a namespace of its own, like this...
namespace codegear {
#include "codegear_header.h"
} // namespace codegear
...then the effects of any using directives within that header are neutralized.
That might be problematic in some cases. That's why every C++ style guide strongly recommends not putting a "using namespace" directive in a header file.
No you can't unuse a namespace. The only thing you can do is putting the using namespace-statement a block to limit it's scope.
Example:
{
using namespace xyzzy;
} // stop using namespace xyzzy here
Maybe you can change the template which is used of your auto-generated headers.
You may be stuck using explicit namespaces on conflicts:
string x; // Doesn't work due to conflicting declarations
::string y; // use the class from the global namespace
std::string z; // use the string class from the std namespace
For future reference : since the XE version there is a new value that you can #define to avoid the dreaded using namespace System; int the include : DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE
How about using sed, perl or some other command-line tool as part of your build process to modify the generated headers after they are generated but before they are used?
Quick experiment with Visual Studio 2005 shows that you can enclose those headers in your own named namespace and then use what you need from this namespace (but don't use the whole namespace, as it will introduces the namespace you want to hide.
#include<iostream>
#include<stdio.h>
namespace namespace1 {
int t = 10;
}
namespace namespace2 {
int t = 20;
}
int main() {
using namespace namespace1;
printf("%d" , t);
printf("%d" , namespace2::t);
}