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;
}
Run Code Online (Sandbox Code Playgroud)
我可以存储一个布尔型标志,但是我想避免它。
它将使用的操作系统是带有gcc 5.4的Ubuntu 16.04。我不能使用POSIX和标准库本身以外的任何库,直到gcc 5.4中存在的版本为止。
我试图阅读手册页open(),close()。他们没有提及要使用的任何特殊价值。
我尝试将其设置为-1,但不确定在所有地方使用是否安全。