Using fully qualified name for std namespace in C++
Asked Answered
I

2

10

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?

Individuality answered 17/2, 2019 at 10:4 Comment(5)
That's because anyone who creates a nested class or namespace named std should be escorted the hell outta the campus the moment they make a commit. So it's not a problem in practice.Valetudinary
@n.m. Excellent ! Accordingly, I've updated the wording of my answer from "Nobody will call a class std" to "Nobody will dare to call a class std" ;-)Lainelainey
@n.m. Is this not worth more elaboration to evolve to answer? Why just a short comment?Drucilla
@Drucilla It's just a joke.Valetudinary
@n.m. It is more. Because it has a point, and actually hits the center point.Drucilla
L
12

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.

Lainelainey answered 17/2, 2019 at 10:41 Comment(3)
namspace mycrazyspace{ namespace std{ /* my psychopath stuff*/};}; Nobody dares do it. But I may be crazy enough to go for it!!!!😈 And the toolchain will not notice any errors.Drucilla
@Drucilla :-D You made my day ! Of course you can do that ! And you even be able to use the global scope resolution operator when needed to disambiguate: at least we'll have someone who'll enjoy that language feature ;-) Joke aside, this feature can also make sense for other namespaces than std, where conflicts are more likely to happen. And I'd guess it'd be mostly used for the creation of less ambiguous aliasesLainelainey
I don't mean to bother, but my point is that the provided answer needs a revision.Drucilla
D
0

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";
}
Dudden answered 17/2, 2019 at 12:55 Comment(1)
Interesting! You could also define a namespace alias, namespace mystd = ::std;. In this case you could just refer to mystd::cout. If later you'd prefer to use your own alternative library, you could change the alias to namespace mystd = test; without changing the rest of the code that uses mystd.Lainelainey

© 2022 - 2024 — McMap. All rights reserved.