How to use specific members of a namespace? - c++

I am trying to use the directive using namespace NS on a specific member but the compiler doesn't seems to understand so.
This is a very simple sample of code :
namespace NS{
int a;
int b;
}
using namespace NS::a;
int main(){
return 0;
}
Clang compiler returns error: expected namespace name at using namespace NS::a;

a is not namespace. So you can just pull contents of namespace in global namespace with using namespace NS, or pull only a in global namespace with using NS::a.

using namespace NS means that NS is a namespace and you want to have direct access to its scope. So instead of NS::a you can write a directly.
While
using namespace NS::a means that NS is a namespace including another namespace a and you want to have access to variables defined in scope of namespace a !!!

Related

shrinking namespace to base

Is there a way to "shrink a namespace"?
e.g.
std::chrono::milliseconds to std::milliseconds by the namespace alone?
i.e.
namespace std = std::chrono
(please disregard the fact that this is modifying std. this is just an example I think everyone can understand)
Add your own alias to std::chrono, external to std:
namespace chrono = std::chrono;
And then use chrono::milliseconds.
It's possible to collapse layers of namespaces by adding an internal namespace alias. The standard library does this:
namespace std {
namespace ranges::views { /* views stuff */ }
namespace views = ranges::views;
}
So that std::ranges::views::filter can also be accesses as std::views::filter.
But that doesn't help here because milliseconds is a type, not a namespace. The only way to collapse the contents of a namespace is with a using-directive (or equivalently, a whole lot of using-declarations):
namespace std {
namespace chrono { /* chrono things */ }
using namespace chrono;
}
Which isn't really a good idea because it could just break name lookup for things - especially if anything in std::chrono is named the same as something in std. This also completely defeats the purpose of having the nested namespace to begin with.
So the equivalent to the inner namespace alias would just be the outer namespace alias:
namespace std {
namespace chrono { /* chrono things */ }
}
namespace chrono = std::chrono;
And now you write chrono::milliseconds instead of std::chrono::milliseconds, without having to break anything in any of those namespaces.
Alternatively, if you really want to just shove everything into the same namespace, do it into a different one:
namespace all {
using namespace std;
using namespace std::chrono;
}
Although, as I said, questionable.
If you have a namespace with some deeply nested name that you want to shorten (and it's a namespace you are allowed to modify). e.g:
namespace my {
namespace deeply {
namespace nested {
namespace ns {
// ...
}}}}
you can simplify it with a namespace alias:
namespace my {
namespace easy = deeply::nested::ns;
}
Is there a way to "shrink a namespace"?
You can give namespace an alias, or you can have using namespace declarations.
e.g.
std::chrono::milliseconds to std::milliseconds
Something like that is possible, but not for the std namespace because you may not put such declarations there.
But this is possible for example:
namespace my {
using namespace std::chrono;
}
and this:
namespace my_chrono = std::chrono;

Difference between 'using' and 'using namespace'

In the boost libraries, there are often examples of including the library like:
#pragma once
#include <boost/property_tree/ptree.hpp>
using boost::property_tree::ptree;
Throughout my program I have been importing namespaces like this:
#include "../MyClass.h"
using namespace MyClassNamespace;
Can someone please explain:
The difference between using and using namespace;
What the advantage of negating the use of using namespace in favour of using;
The differences in forward declaring using and using namespace;
Thanks
using namespace makes visible all the names of the namespace, instead stating using on a specific object of the namespace makes only that object visible.
#include <iostream>
void print(){
using std::cout;
using std::endl;
cout<<"test1"<<endl;
}
int main(){
using namespace std;
cout<<"hello"<<endl;
print();
return 0;
}
while using "using namespace std" all the elements under the scope of std are made available under scope of the function.
while using "using std::cout" we explicitly mention what element under the std is required for the function ,without importing all the elements under std.
this is my first answer in stack overflow please correct me if iam wrong!!

namespace validity in c++

I have a simple question for namespace in C++. There are errors when I compiling the following little piece of code. I don't understand why. Thanks for any help in advance!
#include <iostream>
using namespace std;
int x=100;
namespace first
{
int x=1;
}
namespace second
{
int x=2;
}
int main(){
{
using namespace first;
cout<<x<<endl;
}
{
using namespace second;
cout<<x<<endl;
}
cout<<x<<endl;
}
If I comment out the x declared in the global scope and the last statement. It works fine. But in my mind, the first x is declared in the std namespace and using namespace first and second in the main will be invalid after the the code block they are declared(so the namespace will be std again). So the above code should work. Where am I wrong?
But in my mind, the first x is declared in the std namespace
Your mind is wrong. The using namespace std makes names from std available in the global namespace, it doesn't mean names declared in the global namespace are in std.
x is declared in the global namespace.
so the namespace will be std again
No, nothing in your file is in namespace std, only the contents of <iostream> are in namespace std.
The error is that when you try to use x there are two different variables in scope, ::x and first::x that you could be referring to, so it is ambiguous. You can disambiguate with a using declaration instead of a using directive:
{
using first::x;
cout<<x<<endl;
}
This says that in that scope x refers to first::x
in this case x variable is ambiguous. The compiler can't find which x you are going to use. You can write like this.
#include <iostream>
using namespace std;
int x=100;
namespace first
{
int x=1;
}
namespace second
{
int x=2;
}
int main(){
{
cout<<first::x<<endl;
}
{
cout<<second::x<<endl;
}
cout<<x<<endl;
}
now your code will compile.
The first x is not in the std namespace. It would only be in the std namespace if defined like this:
namespace std
{
int x;
}
Because it's not in that namespace, it's matched by references to x later in the program, and such references become ambiguous when another namespace declaring x is also being used (as in using namespace first / second).

Anonymous Namespace

A recent thread on SO triggerred this.
An anonymous namespace is considered to be equivalent to
namespace unique { /* empty body */ }
using namespace unique;
namespace unique { namespace-body }
I fail to recollect the exact reason as to why it is not equivalent to
namespace unique { namespace-body }
using namespace unique;
Also tried searching (including google) but in vain. Please share any information you have in this regards.
The specification that exists now was introduced in 1995 in N0783 to correct for a corner case. To quote that paper (page 9):
The WP defines the semantics of an unnamed namespace as being equivalent to:
namespace UNIQUE {
// namespace body
}
using namespace UNIQUE;
This is incorrect because it makes the code in an unnamed namespace dependent on
whether the code is in an original namespace or a namespace extension.
namespace {} // If you remove this line, the
// use of ::f below is invalid
namespace {
void f()
{
using ::f;
}
}
The WP should be changed to define an unnamed namespace as being equivalent to:
namespace UNIQUE {}
using namespace UNIQUE;
namespace UNIQUE {
// namespace body
}

using namespace issue

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;