Search code examples
c++namespacesstderrno

Why does NOT exist std::errno while "errno" does?


In order to make my codes more compliant with C++ style instead of C style, I want to replace errno with std::errno, and replace include <errno.h> with include <cerrno>.

To my supprise, these's NO std::errno !

As we known, many C things have an C++ equivalent. For expamle:

In a c-source.c, we code often like this:

#include <string.h>
print( strerro(1) );

In a c++source.cpp, we can do it like this:

#include <cstring>
std::cout << std::strerro(1);

Why is NOT there std::errno while std::strerro exists?


Solution

  • To my supprised, these's NO "std::errno"!

    That is because, errno is a preprocessor macro. It doesn't live in a namespace(actually it has no concept of namespaces etc). From errno documentation:

    errno is a preprocessor macro used for error indication. It expands to a static(until C++11)thread-local(since C++11) modifiable lvalue of type int.

    (emphasis mine)

    This means that we can't prepend errno with std::.