How to access Unnamed namespace variable nested inside named namespace? - c++

This question has alreday discussed in the link
unnamed namespace within named namespace but no perfect answers were provided on how to access the variables of unnamed namespace nested under named namespace in case both variables are same
Consider This Code
namespace apple {
namespace {
int a=10;
int b=10;
}
int a=20;
}
int main()
{
cout<<apple::b; //prints 10
cout<<apple::a; // prints 20
}
Unnamed namespace "variable a" is always hidden. How to access "variable a" of unnamed namespace?
Is it even legal to declare unnamed namespaces inside named namespaces?

unnamed namespace "variable a" is always hidden. How to access "variable a" of unnamed namespace?
It looks like you simply cannot qualify an unnamed namespace outside of the enclosing namespace.
Well, here's how to fix the ambiguity:
namespace apple {
namespace {
int a=10;
}
int getPrivateA() {
return a;
}
int a=20;
}
int main() {
cout<<apple::getPrivateA() << endl;
cout<<apple::a << endl;
}
See the Live Demo.
Though I'm aware that doesn't fully answer your question (besides if it's legal to nest unnamed namespaces inside another namespace).
I'll have to investigate what the c++ standard specification with chapters 3.4 and 7.3 a bit more to give you a definite answer why it's not possible what you want to do.

I read this the other day and have an answer for "How to access "variable a" of unnamed namespace?"
I answer this knowing fully that it isn't a perfect answer, but it is a way to access the "a" from the unnamed namespace.
#include <iostream>
#include <stdio.h>
namespace apple {
namespace {
int a=257;
int b=10;
}
int a=20;
}
using namespace std;
int main() {
int* theForgottenA;
// pointer arithmetic would need to be more modified if the variable before
// apple::b was of a different type, but since both are int then it works here to subtract 1
theForgottenA = &apple::b - 1;
cout << *theForgottenA; //output is 257
}

Related

Can redefining a namespace name in the same scope break the ODR rule?

on C++ primer ch 18. namespaces:
"As with any name, a namespace name must be unique within the scope in which the namespace is defined. Namespaces may be defined at global scope or inside
another namespace. They may not be defined inside a function or a class."
But as I guess if not wrong; defining a namespace more than once in the same scope wouldn't break ODR but instead reopens that namespace and may add to it.
Here is my example:
namespace fooNS{
int x = 5;
}
namespace fooNS{
int y = 10;
}
int main(){
std::cout << fooNS::x << '\t' << fooNS::y << '\n';
}
My code works fine so what is meant with "As with any name, a namespace name must be unique within the scope in which the namespace is defined."?
So are there some situations where redefining the same namespace will break the ODR rule? Thanks!

Using fully qualified name for std namespace in C++

If name in C++ is not fully qualified, e.g. std::cout, it can lead to an unintentional error, such as mentioned at https://en.cppreference.com/w/cpp/language/qualified_lookup. But using a fully qualified name for ::std namespace, e.q. ::std::cout, is very rare, as I have noticed.
Is there any reason why a fully qualified name for ::std namespace is not used?
And what about using fully qualified name for own created namespaces? Is it good idea?
You are completely right, in the sense that yyyy::xxx can be ambiguous if there is a namespace yyyy and also a class yyyy which are both visible in the same scope. In this case only the full qualification ::yyyy::xxx can solve the ambiguity. The example of your link makes it very clear:
// from cppreference.com
#include <iostream>
int main() {
struct std{};
std::cout << "fail\n"; // Error: unqualified lookup for 'std' finds the struct
::std::cout << "ok\n"; // OK: ::std finds the namespace std
}
But in practice, it's difficult to create a conflicting std at top level, since most of the includes from the standard library will make it fail:
#include <iostream>
struct std { // OUCH: error: ‘struct std’ redeclared as different kind of symbol
int hello;
};
This means that to create a conflict, you'd need to define local classes or introduce a using clause in another namespace. In addition, nobody will (dare to) call a class std.
Finally, in practice, ::yyyy::xxx is less convenient to read. All this explains why you won't find it very often.
Additional remark
The problem is not so much for std which is well known, but rather for your own namespaces and third party libraries. In this case, the namespace alias would be a better alternative to :::yyyy to disambiguate:
namespace foo {
void printf() { }
}
int main() {
foo::printf(); // ok, namespace is chose because no ambiguity
struct foo {/*...*/ }; // creates ambiguity
//foo::printf(); // error because struct foo is chosen by name lookup
::foo::printf(); // ok, but not if you decide to move the code to be nested in another namespace
namespace mylib = foo ; // or ::foo (see discussion below)
mylib::printf(); // full flexibility :-)
}
Its advantage is a higher flexibility. Suppose for example that you'd move your code to nest it in an enclosing namespace. With the namespace alias, your code could continue to work as is (in the worst case with a minor adjustment in the alias definition). With the global scope resolution, you'd have to change all the statements where the global namespace ::foo would be used.
To maintain big code or better readability or clashes in names, C++ has provided namespace " a declarative region".
A namespace definition can appear only at global scope, or nested within another namespace.
#Sample Code
#include <iostream>
int main()
{
struct std{};
std::cout << "fail\n"; // Error: unqualified lookup for 'std' finds the struct
::std::cout << "ok\n"; // OK: ::std finds the namespace std
}
In the above code compiler is looking for cout in struct std , but in next line when you use ::std::cout it looks for cout in globally defined std class.
Solution:
#include <iostream>
//using namespace std; // using keyword allows you to import an entire namespace at once.
namespace test
{
void cout(std::string str)
{
::std::cout<<str;
}
}
int main()
{
cout("Hello");//'cout' was not declared in this scope
::test::cout("Helloo ") ;
::std::cout<<"it is also ok\n";
}
Or use the in this way , it is just for better readability
##
using namespace test;
int main()
{
cout("Hello");//'cout' was not declared in this scope
cout("Helloo ") ;
::std::cout<<"it is also ok\n";
}

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

"using namespace" statement inside an anonymous namespace

When using a using namespace statement inside an anonymous namespace bring the namespace used in to the file scope? Eg:
namespace foo
{
int f() { return 1; }
}
namespace
{
using namespace foo;
}
int a()
{
return f(); // Will this compile?
}
According to 7.3.4 [namespace.udir] paragraph 4 a namespace directive is transitive:
For unqualified lookup nominates a second namespace that itself contains using-directives, the effect is as if the using-directives from the second namespace also appeared in the first.
... and according to 7.3.1.1 [namespace.unnamed] paragraph 1 there is kind of an implicit using directive for the unnamed namespace:
An unnamed-namespace-definition behaves as if it were replaced by
inline namespace unique { /* empty body */ }
using namespace unique ;
namespace unique { namespace-body }
where inline appears if and only if it appears in the unnamed-namespace-definition, all occurrences of unique in a translation unit are replaced by the same identifier, and this identifier differs from all other identifiers in the entire program.
Thus, the answer is "yes, this is supposed to compile" (and it does with all C++ compilers I tried it with).
Yes.
This is because an anonymous namespace is automatically brought into the containing scope.
Yes, because, as Dietmar Kühl quoted, an anonymous namespace is replaced by its content.
However, you should pay attention that it is replaced exactly where it is declared (edit), so there is no "magic" in this. For example, this won't work:
namespace foo
{
int f() { return 1; }
}
int a()
{
return f(); // Will this compile?
}
namespace
{
using namespace foo;
}

What is the need to specify "std" prefix?

I'm a beginner in programming and I often see many programs using the prefix std if they are using any std functions like std::cout, std::cin, etc. I was wondering what is it's purpose ? Is it just a way of good programming or is there more to it ? Does it make any difference for the compiler or is it readability or what ? Thanks.
The STL types and functions are defined in the namespace named std. The std:: prefix is used to use the types without fully including the std namespace.
Option 1 (use the prefix)
#include <iostream>
void Example() {
std::cout << "Hello World" << std::endl;
}
Option #2 (use the namespace)
#include <iostream>
using namespace std;
void Example() {
cout << "Hello World" << endl;
}
Option #3 (use types individually)
#include <iostream>
using std::cout;
using std::endl;
void Example() {
cout << "Hello World" << endl;
}
Note: There are other implications to including an entire C++ namespace (option #2) other than not having to prefix every type / method with std:: (especially if done within a header) file. Many C++ programmers avoid this practice and prefer #1 or #3.
C++ has a concept of namespaces.
namespace foo {
int bar();
}
namespace baz {
int bar();
}
These two functions can coexist without conflict, since they're in different namespaces.
Most of the standard library functions and classes live in the "std" namespace. To access e.g. cout, you need to do one of the following, in order of preference:
std::cout << 1;
using std::cout; cout << 1;
using namespace std; cout << 1;
The reason you should avoid using is demonstrated with the above foo and baz namespaces. If you had using namespace foo; using namespace baz; any attempt to call bar() would be ambiguous. Using the namespace prefix is explicit and exact, and a good habit.
Nobody mentioned in their answer that a using namespace foo statement can be put inside a function body, thereby reducing namespace contamination in other scopes.
For example:
// This scope not affected by using namespace statement below.
void printRecord(...)
{
using namespace std;
// Frequent use of std::cout, io manipulators, etc...
// Constantly prefixing with std:: would be tedious here.
}
class Foo
{
// This scope not affected by using namespace statement above.
};
int main()
{
// This scope not affected either.
}
You can even put a using namespace foo statement inside a local scope (pair of curly braces).
It's a C++ feature called namespaces:
namespace foo {
void a();
}
// ...
foo::a();
// or:
using namespace foo;
a(); // only works if there is only one definition of `a` in both `foo` and global scope!
The advantage is, that there may be multiple functions named a - as long as they are within different namespaces, they can be used unambiguously (i.e. foo::a(), another_namespace::a()). The whole C++ standard library resides in std for this purpose.
Use using namespace std; to avoid the prefix if you can stand the disadvantages (name clashes, less clear where a function belongs to, ...).
It's short for the standard namespace.
You could use:
using namespace std
if you don't want to keep using std::cout and just use cout