namespace CounterNameSpace {
int upperbound;
int lowerbound;
using namespace NS;//Error
}
namespace NS {
int i;
}
// ...
namespace NS {
int j;
}
In the above case it shows an error .
error C2871: 'NS' : a namespace with this name does not exist
I know if i define NS before counternamespace problem will be solved . But just want to know whether any thing like forward declaration of namespace exist in c++ or not .So that the above problem will be resolved without defining NS before counternamespace .
please help .
Nothing says a namespace needs all of its contents right away:
namespace NS {}
namespace CounterNameSpace {
int upperbound;
int lowerbound;
using namespace NS;
}
namespace NS {
int i;
}
However, this might not do what you want. You still won't be able to use any of the types in that namespace until you've declared them.
Related
So i work on this library (header only) that has several modules and uses an inline namespace for versioning. Thus the fully qualified names can get a bit longer at times. I would like to use 'using namespace x' as an implementation detail inside the (inline) detail namespaces, but that pollutes the outer namespace.
See the example code on godbolt: https://godbolt.org/z/pdSXrs
I found two workarounds (see godbolt link) that do what I want, but seem to be overly complicated. Is there an easier way to do this? Should I just type out all the fully qualified names (and keep the error messages cleaner)? I would love to know what are the best practices for 'more complicated' namespace constructs.
Edit: added code as suggested
// think of these as in header files
namespace mylib{
namespace module0{
inline namespace mystuff{
// this constant conceptually belongs to module 0 but might be used as an
// implementation detail in other modules
int myconst = 42;
}
}
}
namespace mylib{
namespace module1{
// I would like to use this, but pollutes mylib::module1 namespace
inline namespace detail1{
using namespace mylib::module0::mystuff;
struct foo1{
int operator()(){
return myconst;
}
};
}
}
}
namespace mylib{
namespace module2{
inline namespace detail1{
// this works but seems overly complicated
namespace more_detail{
using namespace mylib::module0::mystuff;
struct foo2{
int operator()(){return myconst;}
};
}
// might be very cumbersome to use if lots of classes
using more_detail::foo2;
}
}
}
namespace mylib{
namespace module3{
inline namespace detail1{
// is this a good way to namespace a library...?
namespace more_detail{
using namespace mylib::module0::mystuff;
// not enough namespaces yet?!
namespace devil_of_details{
struct foo3{
int operator()(){return myconst;}
};
}
}
// useable for lots of classes/types in that namespace... but really?!
using namespace more_detail::devil_of_details;
}
}
}
// think of this as myprogram.cpp including the headers
#include <iostream>
int main(){
// polluted namespace. I can probably live with it, but it's not ideal
// constant should only be visible in module0
int x1 = mylib::module1::myconst;
std::cout << x1 << std::endl;
// ok
int x0 = mylib::module0::myconst;
// this is what I ideally want, i.e. not polluting module2 namespace
int x2_error = mylib::module2::myconst;
// this is what I ideally want, i.e. not polluting module3 namespace
int x3_error = mylib::module3::myconst;
// normal usage in cpp file
using namespace mylib::module2;
int x2 = foo2{}();
std::cout << x2 << std::endl;
// ok
using namespace mylib::module0;
std::cout << myconst << std::endl;
}
I would suggest namespace aliases.
namespace mylib{
namespace module1{
namespace details0 = module0::mystuff;
}
}
While this makes the other namespaces visible, it does not actually import the symbols the way a using does, that is mylib::module1 does not have a myconst member.
Also, note that so long as the namespace you are currently defining is part of mylib, there is no need to have mylib as part of the name. Even if you close mylib then re-open it.
That is:
namespace mylib {
namespace part1 { /* ... */ }
}
namespace mylib {
namespace part2 { namespace p1 = part1; }
}
p1 will refer to mylib::part1 so long as you don't specify that it is a fully-qualified name (::part1).
Why I get error message (error: call of overloaded myCout() is ambiguous) when I use two different namespaces in same function making use of using namespace directive without fully qualified name space?
#include <iostream>
using namespace std;
namespace first
{
void myCout(void)
{
cout<<"Hello World is great\n";
}
}
namespace second
{
void myCout(void)
{
cout<<"Hello Sky is high\n";
}
}
int main(void)
{
cout<<"Hello World\n";
using namespace first;
myCout();
using namespace second;
myCout();
return(0);
}
If I use fully qualified namespaces for myCout() in second namespace as given below, there is no issue
int main(void)
{
cout<<"Hello World\n";
using namespace first;
myCout();
second::myCout();
return(0);
}
using directives respect scope. So you can introduce a new block scope to limit the availability of the symbols introduced by each:
int main(void)
{
cout<<"Hello World\n";
{
using namespace first;
myCout();
}
{
using namespace second;
myCout();
}
return(0);
}
Normally, and so as to avoid conflicts and deep nesting, try to pull in just the identifiers you need with a using declaration instead. If for instance you only ever wanted to use class foo from first then there would be no ambiguity with the following:
using first::foo;
using namespace second;
using namespace ... directives do not create an ordered path; nor do they override all previous ones. So, yes, your code creates an ambiguous situation.
First you use namespace first, thus the myCout of the first namespace is introduced. Then you use namespace second, causing the other myCout to be come into play too. The second namespace does not override the previous namespace.
As a result, when you call myCout for the second time, there are two definitions into play, causing the compiler to rule this as an ambiguous call.
In other words:
int main(void)
{
using namespace first; // `myCout` of the 1st namespace is introduced
myCout();
using namespace second; // `myCout` of the 2nd namespace is introduced
// and does not override the first namespace!
myCout(); // Which `myCout`? Of the first or second?
return 0;
}
For eg., when using OpenCV, we specify
using namespace cv;
But where does C++ look down to know where it is defined?
using namespace will not make everything declared in that namespace visible. It will expose only what translation unit "sees".
Consider following code
One.h
#pragma once
namespace ns
{
void function1();
}
Two.h
#pramga once
namespace ns
{
void function2();
}
main.cpp
#include "Two.h" // <-- included only Two.h
using namespace ns;
int main()
{
function2(); // <-- is a ns::function2() located in Two.h
function1(); // <-- error compiler does not know where to search for the function
return 0;
}
What happened here is the compiler created translation unit with all preprocessor directives resolved. It will look something like this:
namespace ns
{
void function2();
}
using namespace ns;
int main()
{
function2(); // <-- OK. Declaration is visible
function1(); // <-- Error. No declaration
return 0;
}
How does C++ know where to look for the namespace specified using using namespace …?
It doesn't.
When you use
using namespace cv;
the scope of search for names (of classes, functions, variables, enums, etc) is expanded. The names are searched in the cv namespace in addition to other scopes in which they are normally searched.
This question already has answers here:
why prepend namespace with ::, for example ::std::vector
(5 answers)
Closed 7 years ago.
using ::std::...;
VS
using std::...;
Is there a difference(s)? If so, which one(s)?
I saw this:
using ::std::nullptr_t;
which made me wonder.
In your case, there is most likely no difference. However, generally, the difference is as follows:
using A::foo; resolves A from the current scope, while using ::A::foo searches for A from the root namespace. For example:
namespace A
{
namespace B
{
class C;
}
}
namespace B
{
class C;
}
namespace A
{
using B::C; // resolves to A::B::C
using ::B::C; // resolves to B::C
// (note that one of those using declarations has to be
// commented for making this valid code!)
}
If you are inside another namespace that has its own nested std namespace, then ::std and std are different. A simple example:
#include <iostream>
namespace A {
namespace std {
void foo() { ::std::cout << "foo" << ::std::endl;}
}
//using std::cout; // compile error
using ::std::cout; //ok
using std::foo; // ok
//using ::std::foo; // compile error
}
Though definitely not a good practice to ever have a nested std namespace.
From: http://en.cppreference.com/w/cpp/language/using_declaration
Using-declaration introduces a member of another namespace into
current namespace or block scope
Therefore, if your current scope already has a class with the same name, there will be an ambiguity between the one you introduced and the one in your current namespace/block.
A using declaration is just a subset of a using directive. The using directives is defined as follows (http://en.cppreference.com/w/cpp/language/namespace):
From the point of view of unqualified name lookup of any name after a
using-directive and until the end of the scope in which it appears,
every name from namespace-name is visible as if it were declared in
the nearest enclosing namespace which contains both the
using-directive and namespace-name.
Thus, you can consider these two examples that display the issues that can arise.
It prevents ambiguity between namespaces that share the same name (example 1) as well as ambiguity between class names in different namespaces (example 2).
namespace A
{
namespace B
{
struct my_struct {};
}
}
namespace B
{
struct my_struct {};
}
using namespace A; // removing this line makes B:: resolve to the global B::
int main()
{
::B::my_struct; // from global, will not pick A::B::
B::my_struct; // error: 'B' is ambiguous, there is A::B:: and B::
}
Consider this example which showcases why people shun the use of using namespace std;
using namespace std;
template <typename T>
class vector
{ };
int main()
{
vector<int> v; // which one did you want? ambiguous
::vector<int> v_global; // global one
::std::vector<int> v_std; // std::vector<T>
}
It depends on where you use the using declaration. On a global namespace scope there will be no difference. However, if you have code like
#include <iostream>
#include <vector>
namespace my_namespace {
namespace std {
class vector {
};
}
using std::vector;
}
int main()
{
my_namespace::vector<int> v;
}
it will not compile unless you inform the compiler to search the global namespace -> std namespace -> vector in your declaration by stating using ::std::vector.
I'm trying to understand why this program does not give an name-lookup ambiguity for i:
namespace X { int i = 1; }
namespace Q {
namespace P {
int i = 2;
using namespace X;
}
using namespace P;
int l = i;
}
int main() {}
If we modify it like this we get a name-lookup ambiguity:
namespace X { int i = 1; }
namespace P {
int i = 2;
using namespace X;
}
using namespace P;
int l = i;
int main() {}
The only change I made here is to remove the namespace Q and place it's content in the global namespace instead.
I have tried with 3 different compilers:
GCC and Clang with http://melpon.org/wandbox
visual c++ with http://webcompiler.cloudapp.net/
The all give the results stated in this email, and i'm trying to find out why.
Can anyone explain the behaviour in terms of the c++ standard? I fail to understand it.
In the first program used variable i is defined in namespace P because the using directive
using namespace X;
places declarations of X in the global namespace (the common namepsace for X and P). Thus the declaration of i in P (more precisely in Q due to another using directive) hides the declaration of X::i in the global namespace.
From the C++ Standard (3.4.1 Unqualified name lookup)
2 The declarations from the namespace nominated by a using-directive
become visible in a namespace enclosing the using-directive; see
7.3.4.
So we have for the first program
namespace X { int i = 1; }
namespace Q {
namespace P {
int i = 2;
using namespace X; // 1
}
using namespace P; // 2
int l = i;
}
that the enclosing namespace for using directive #1 is the global namespace and the enclosing namespace for using directive #2 is the namepsace Q.
In the second program the both definitions of i are placed in the global namespace due to these two using directives
//...
using namespace X;
//...
using namespace P;