Bad practice to declare names in a standard namespace?

I looked at the Google C ++ style guide and came across this:

"Do not declare anything in the std namespace, or even forward declarations of standard library classes. Declaring objects in the std namespace is undefined behavior, that is, not portable. To declare objects from the standard library, include the appropriate header file."

Can someone explain what this means and why this behavior is undefined using sample code?

+6
source share
5 answers

Can someone explain what this means and why this behavior is undefined using sample code?

The following program gives undefined behavior:

namespace std { void foo(int) { } } #include <iostream> int main() { std::cout << "Hello World!" << std::endl; } 

Why? It declares a function named foo in the std . As for the reason why this might cause a problem, consider: the standard library implementation may have its own function called foo() and which can be used by some component in <iostream> . Then your foo might be better than implementing the standard foo library.

+11
source

First of all, like many of the Google style guides, this is actually wrong. The standard specifically allows you to define several specific objects in the std namespace (for example, the specialization of existing templates over user-defined types).

Ignoring these exceptions, they have the correct general idea - your code usually belongs somewhere other than the std namespace. You can put it in a global namespace or define a different namespace, but you should leave only std .

You should also not try to forward-declare anything in the standard library. Standard functions are allowed (for example) to include additional parameters if they include default values, so they can be called in a standard way. If you try to declare them yourself, instead of declaring an existing function, you can end up declaring an ambiguous overload.

Bottom line: yes, use the standard library. When you use it, get ads by including standard headlines, rather than trying to write your own.

+10
source

This means not to declare your own types in the std . You can use the standard library, but you must do this by including the appropriate header.

Basically, make sure all of your declarations are in your own namespace, not std .

+6
source

They say that you should not forward ads from the standard library as follows:

 // myheader.h namespace std{ template<class T> void SomeStandardFunction(); } // use std::SomeStandardFunction 

Instead, you should simply include the header directly:

 // myheader.h #include <SomeHeaderThatContainsSomeStandardFunction> // use std::SomeStandardFunction 
+4
source

This does not say "Do not use the standard library."

To use something and declare something are two different things. It says that do not declare anything, since do not do something like "class ostream;". I assume that people used to declare it so as to use it, but now, since things are declared in the std namespace, you just include the header file.

Mark this one out.

+2
source

Source: https://habr.com/ru/post/886029/


All Articles