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);
}
Related
To keep class definition in header file clean, I decided to move implementations of templated functions into another *.h file, which I include in the main header. Now I would like to make use of using namespace there, to make code more readable.
But using namespaces would affect the whole application, since the file gets included in the header which itself gets included in the whole application. The using of the namespaces would spread out of the file.
How can I handle this?
You can put using namespace XX inside function definitions to scope the using declaration to that function:
int func( ...args... )
{
using namespace std;
// ... body of function
}
Use a namespace alias.
namespace submod_ = topspace::other_project::module::submodule;
Then you can use submod_ wherever you would require the very long namespace.
This requires you to still use submod_ where you would have the long namespace qualifier. In that sense, it doesn't exactly answer your question. But I would argue that the explicitness of the qualification aids readability and helps prevent mistakes.
There are real examples of StackOverflow questions where a "using" declaration brought in "lurking" functions which the code author did not realize.
You can place the using namespace in the namespace of your 'main header':
Header.h
#include <string>
namespace Test
{
using namespace std;
string f()
{
return "Test";
};
}
main.cpp
#include "Header.h"
int main()
{
Test::f();
string test; // Error: identifier "string" is undefined
std::string test;
return 0;
}
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.
With using namespace I make the whole contents of that namespace directly visible without using the namespace qualifier. This can cause problems if using namespace occurs in widely used headers - we can unintendedly make two namespaces with identical classes names visible and the compiler will refuse to compile unless the class name is prepended with the namespace qualifier.
Can I undo using namespace so that the compiler forgets that it saw it previously?
No, but you can tell your coworkers that you should never have a using directive or declaration in a header.
As others said, you can't and the problem shouldn't be there in the first place.
The next-best thing you can do is bring in your needed symbols so that they are preferred by the name look-up:
namespace A { class C {}; }
namespace B { class C {}; }
using namespace A;
using namespace B;
namespace D {
using A::C; // fixes ambiguity
C c;
}
In some cases you can also wrap the offending includes with a namespace:
namespace offender {
# include "offender.h"
}
No, C++ Standard doesn't say anything about "undo". The best you are allowed to do is to limit scope of using:
#include <vector>
namespace Ximpl {
using namespace std;
vector<int> x;
}
vector<int> z; // error. should be std::vector<int>
But unfortunately using namespace Ximpl will bring all names from std namespace as well.
Not to my knowledge... But as a rule I only use "using namespace" in .cpp files.
The closest, that I'll try to use in header files is following:
//example.h
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
/**
* hating c++ for not having "undo" of using namespace xx
*/
#define string std::string
#define map std::map
class Example {
public:
Example (const char *filename);
Example (string filename);
~Example ();
private:
map<string,complicated_stuff*> my_complicated_map;
};
#undef string
#undef map
#endif //EXAMPLE_H_
after all, defines are #undef -able.
There are 2 problems:
1. it is ugly
2. separate #define and #undef for each name from the corresponding namespace are used
As stated you should not use using namespace sth in header files. When you need functionality from a namespace in your implementation you can leverage scopes like this:
void func() {
// some code agnostic to your namespace.
{
using namespace sth;
// some code aware of sth.
}
// some other code agnostic to your namespace.
}
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.
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);
}