Why is namespace std not required for legacy C identifiers?

This code is correct C++:

#include <ctime>
int main()
{
   std::time_t t = std::time(nullptr);
}

However, this compiles fine too (GCC 5.2):

#include <ctime>
int main()
{
   time_t t = time(nullptr);
}

More generally, it seems that legacy "C" data types and functions don't require namespace qualifying.

It seems to me that this is a dangerous behaviour, as both are accepted and the possibility of name collision is still there. I thought (erroneously ?) that the standard namespace std was there to protect me against this.

So my question is: why did the standardization committee allow such a behaviour in C++11 ? Am I wrong in my analysis ?

I understand the issues about legacy code, but I though the ".h" header files ( iostream.h , ...) were there specifically to address this point.

Edit : the linked question is not a duplicate, it asks about if one should or not use the std:: version of legacy functions. What I want to know is the rationale behind this behaviour.


Since C++11, implementations are formally allowed to put C standard library names defined in <cxxx> headers in the global namespace. This doesn't mean that they are required to, so your second code sample may fail on a different platform.

So to say that std is not required for C identifiers is not entirely correct. It may not be required on some implementations, that is all.

Note that before C++11, many implementations did it anyway, although technically they weren't supposed to.

链接地址: http://www.djcxy.com/p/29244.html

上一篇: 调试gf3 /沙箱模块

下一篇: 为什么传统C标识符不需要名称空间标准?