Your question can be divided in two:
Why is POSIX file descriptor int
?
Like most of things in already established tools and libraries, the answer is probably historical reasons. James' answer points this out.
Making the file descriptor opaque is probably a good idea, but not for the reason you mentioned. Making the type opaque is good for having a different type based on some parameters. For example, on some systems you may want a long long
as file descriptor. However, as it seems to happen, no one nowhere has needed 2 billion open files at the same time and therefore no one has cared to fix this non-existing problem.
On the other hand, such a thing as typedef int file_descriptor;
won't fix any of the problems you mentioned above:
It means that they're easy to confuse for other integers...
If you confuse your variables, I have bad news for you. The compiler won't help you either since file_descriptor
and int
are the same type, so any operation on one is allowed on the other.
... and there's no way of knowing without context what they are, what they point to, whether they're open, etc.
You can't do that with FILE
either. That's why you have functions that query the information you seek and return it, just like with FILE
. typedef
ing the type won't give you any extra information.
Why doesn't POSIX have a C++ wrapper for it?
In short, because except Microsoft, no operating system developer is in love with C++. Windows is barely even POSIX, so there is no hope for Microsoft in trying to improve anything POSIX. Other operating systems which are POSIX-compliant have a C API as the de facto system programming language (partly because as n.m. says, almost all languages can bind to C).
In fact, C++ is popular among application developers, but not as much among system programmers. The POSIX committee doesn't seem to be particularly interested in C++ either. That's why you'd see C and only C solutions and arguments with respect to the POSIX API. Also note that POSIX was created to standardize UNIX's interface in particular, which was written in C and one of its most important descendants, Linux, is also strongly bound to C.