using namespace issue - c++

When I use the following
#include <map>
using namespace LCDControl;
Any reference to the std namespace ends up being associated with the LCDControl name space.
For instance:
Generic.h:249: error: 'map' is not a member of 'LCDControl::std'
How do I get around this? I didn't see anything specific to this on any documentation I looked over. Most of them said not to use: using namespace std;.
Here's line 249:
for(std::map<std::string,Widget *>::iterator w = widgets_.begin();

It looks like there's a std namespace within LCDControl that's hiding the global std namespace. Try using ::std::map instead of std::map.
I would say that either there's a using namespace std somewhere within the LCDControl namespace, or possibly there's an #include of a STL header that defines std within the LCDControl namespace.
e.g.:
namespace LCDControl
{
#include <map>
}
Which would define all the symbols in <map> as part of LCDControl::std, which in turn would hide the global std, or at least any symbols defined in the inner namespace, I'm not sure.
When I tried this under VS2008, I got an error:
namespace testns
{
int x = 1;
}
namespace hider
{
namespace testns
{
int x = 2;
}
}
int y = testns::x;
using namespace hider;
int z = testns::x; // <= error C2872: 'testns' : ambiguous symbol

The 'map' class lives in the std namespace, so you are going to have to qualify that somewhere. How are you qualifying your map object? You should have no problem doing this:
std::map<foo> myMap;
You can also do something like this if you do not want to explicitly qualify it every time, but also do not want to pollute your global namespace:
using std::map;

Related

What is "namespace clt and dmp"?

I'm trying to implement a vector in c++.
Found this online: http://doingmyprogramming.com/2013/06/25/implementing-a-vector-in-c-working-smarter-not-harder/
I'm confused as to what namespace clt & namespace dmp are.
Thanks!
These are namespaces. In this case, they are structured this way:
global
-> clt
-> dmp
If you look few lines lower, you will see this code:
namespace clt {
namespace dmp {
template <typename T>
class vector {
..words..
};
}
}
It defines class vector<T> inside namespace dmp, which is itself defined inside namespace clt. So, when you are in global namespace (let's say, main() function), you must use explicit scope operator to be able to use vector. Example:
int main()
{
clt::dmp::vector<int> vecint; //Create vector of ints
//...
}
The most common example of a namespace is namespace std, which contains C++ standard library primitives.
Oh, and if you are concerned about their names (clt and dmp) - they are probably just abbreviations.

Is it ok to wrap code in anonymous namespaces for using directives? [duplicate]

This question already has answers here:
Is using namespace in an anonymous namespace safe?
(2 answers)
Closed 3 months ago.
I want using namespace std; to apply to classes as well as functions without polluting the global namespace, but I'm wondering if it's an ok approach.
namespace
{
using namespace std;
class S
{
public:
S()
{
cout << "ok";
}
friend ostream operator<<(ostream& os, const S& s);
};
}
Any caveats to this?
It will work but keep in mind the following points:
You should limit its use in a source file, not in a header file (in general you should refrain from using unnamed namespaces in headers since they can easily mess around with your symbol definitions, especially if there are inline functions that use something from the anonymous namespace).
It's a bad practice and adding an additional naming hierarchy layer (i.e. the anonymous namespace) just for laziness is as bad as it sounds.
If it's in header file then it's not preferable as this file could be included in multiple source file. If in some source file then its acceptable
Whilst this looked like a great solution, in my experiments it doesn't do as I expected it would. It doesn't limit the scope of the using directive.
Consider this:
#include <string>
namespace {
string s1; // compile error
}
namespace {
string s2; // compile error
}
string s3; // compile error
int main()
{
}
None of those strings compile because they are not properly qualified. This is what we expect.
Then consider this:
#include <string>
namespace {
using namespace std;
string s1; // compiles fine (as expected)
}
namespace {
string t2; // compiles fine (I didn't expect that)
}
string v3; // compiles fine (I didn't expect that either)
int main()
{
}
So placing a using directive within an unnamed namespace appears to be exactly the same as placing it in the global namespace.
EDIT: Actually, placing a symbol in an unnamed namespace makes it local to the translation unit. So this is why it can't work for the intended purpose.
Therefore it has to be a no-no for headers.
An unnamed namespace doesn't contain the effects of a using-directive:
namespace A {int i;}
namespace {
using namespace A;
}
int j=i,k=::i; // OK
Both qualified and unqualified lookup follow the implicit using-directive for the unnamed namespace and then follow the explicit one within it. Wrapping the unnamed namespace inside another namespace limits the unqualified case to other code within that outer namespace, of course:
namespace A {int i;}
namespace B {
namespace {
using namespace A;
}
int j=i; // OK, as above
}
int j=i, // error: i not found
k=B::i; // OK
However, in either case you might as well write the using-directive outside the unnamed namespace (or not write it at all if it would be problematic, perhaps because it would appear in a header file).
Don't take the decision to use an anonymous namespace just so you can limit the scope of a using directive.
That being said, if an anonymous (or any other) namespace is already there and you want the advantages of the using inside it, it's fine as long as your coding standards okay it.

Functions in namespace std accessible in global scope

Under some situations, it seems like I can access functions that should be in the std namespace without a using or std:: qualifier. So far, I've only seen this occur with functions from the algorithm library.
In the following example, I expect all_of() to be in the std namespace, but this code compiles without error in VS2013 (Microsoft Compiler 18).
#include <iostream>
#include <string>
#include <algorithm>
int main() {
const std::string text = "hey";
std::cout << all_of(begin(text),end(text),islower);
return 0;
}
Changing std::cout to cout without adding a using namespace std or using std::cout generates an "undeclared identifier" error as expected.
What's going on here?
This probably happens due to Argument-Dependent Lookup. The iterator returned by begin(text) and end(text) is probably a class defined in namespace std (or nested in a class in namespace std), which makes namespace std associated with it. Looking up unqualified names for function calls looks into associated namespaces, and finds all_of there.
By the way, this is exactly the same reason why calling begin(text) works, even though the function template begin() is defined in namespace std. text is a std::basic_string, so std is searched.

Use of "using" in header files

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.

Can I undo the effect of "using namespace" in C++?

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.
}