开发者

Bad practice to declare names in the standard namespace?

I was looking through the G开发者_Python百科oogle C++ style guide, and came across this:

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

Could someone explain what this means and why this is undefined behavior using example code?


Could someone explain what this means and why this is undefined behavior using example code?

The following program yields 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 namespace std. As for why this could cause a problem, consider: the Standard Library implementation might have its own function named foo() and that might be used by some component in <iostream>. Your foo might then be a better match than the Standard Library implementation's foo.


First of all, like much of the Google style guide, it's actually wrong. The standard specifically allows you to define a few specific entities in namespace std (e.g., specializations of existing templates over user defined types).

Ignoring those exceptions, however, they do have the right general idea -- your code normally belongs somewhere other than namespace std. You can put it in the global namespace, or you can define another namespace, but you should leave std alone.

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

Bottom line: yes, use the standard library. When you do use it, get the declarations by including the standard header(s), not by trying to write your own.


This is saying not to declare your own types in the std namespace. You can use the standard library, but you should do so by including the appropriate header.

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


They're saying that you shouldn't forward declare stuff from the standard library like this:

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

// use std::SomeStandardFunction

Instead, you should just include the header directly:

// myheader.h
#include <SomeHeaderThatContainsSomeStandardFunction>

// use std::SomeStandardFunction


This isn't saying "Do not use the standard library".

To use something, and to declare something, are two different things. It's saying don't declare anything as in don't do something like "class ostream;". I guess people used to have to declare it like that in order to use it, but now, since things are declared in namespace std, you simply include the header file.

Check this out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜