POSIX 中是否存在空文件描述符值?

Is there a null file descriptor value in POSIX?

tl;dr 是否有一个 posix 描述符值,我可以将其放入 close() 而什么也不会发生?


是否有我可以使用的特定值,例如 NULL 用于指针、文件描述符?我希望代码统一,所以我想我可以将原始描述符设置为空描述符。

class socket
{
    int fd;
public:
    //deleted copy operations
    socket(socket&& other):
                   fd{other.fd}
    {
        other.fd = /*null descriptor*/
    }

    ~socket()
    {
        if (close(fd) == -1)
        {
            throw std::runtime_error{strerror(errno)};
        }
        // ^^ null file descriptor will do nothing on close()
        // like delete nullptr;
    }

我可以存储一个布尔标志,但我想避免它。

将使用的 OS 是 Ubuntu 16.04,gcc 5.4。我不能使用 POSIX 和标准库本身之外的任何库,直到 gcc 5.4.

中存在的版本

我尝试阅读 open()close() 的手册页。他们没有提到要使用的任何特殊值。

我试过将它设置为-1,但我不确定在任何地方使用它是否安全。

可以在 close 调用中使用文件描述符的负 1 值,除了关闭返回 -1 本身外,对应用程序没有不利影响。

因为保证负一永远不会是有效的文件描述符,所以它是安全的。