我可以使用 nullptr 作为 Linux 系统调用参数吗?
Can I use nullptr as a Linux system call parameter?
我正在使用 C++11 在 Raspbian 上开发用户 space 应用程序。
ReSharper++ 建议我在以下系统调用中使用 nullptr
而不是 NULL
:
auto ret = timerfd_settime(this->_timer_fd, 0, &timeout, NULL);
assert(0 == ret);
我认为我应该继续使用 NULL
,因为这个库专用于 C 系统编程。
这种情况下的最佳做法是什么?
NULL
和 nullptr
都可以正常工作。但是,由于这是 C 中的 syspro,我会选择 NULL
,因为这是我在该领域更经常(如果不是总是)看到的。
如果你检查你的系统调用的 reference,你会看到到处都提到了 NULL
,而不是 nullptr
,这是你应该期待的(如果没有确保你将阅读以下答案)。
同时检查 NULL vs nullptr。
我会继续使用 nullptr,因为意图更明确。
对于任何类型,nullptr 都可以隐式转换为指针,因此绝对安全。
cppreference.com:
The keyword nullptr denotes the pointer literal. It is a prvalue of
type std::nullptr_t. There exist implicit conversions from nullptr to
null pointer value of any pointer type and any pointer to member type.
我正在使用 C++11 在 Raspbian 上开发用户 space 应用程序。
ReSharper++ 建议我在以下系统调用中使用 nullptr
而不是 NULL
:
auto ret = timerfd_settime(this->_timer_fd, 0, &timeout, NULL);
assert(0 == ret);
我认为我应该继续使用 NULL
,因为这个库专用于 C 系统编程。
这种情况下的最佳做法是什么?
NULL
和 nullptr
都可以正常工作。但是,由于这是 C 中的 syspro,我会选择 NULL
,因为这是我在该领域更经常(如果不是总是)看到的。
如果你检查你的系统调用的 reference,你会看到到处都提到了 NULL
,而不是 nullptr
,这是你应该期待的(如果没有确保你将阅读以下答案)。
同时检查 NULL vs nullptr。
我会继续使用 nullptr,因为意图更明确。
对于任何类型,nullptr 都可以隐式转换为指针,因此绝对安全。
cppreference.com:
The keyword nullptr denotes the pointer literal. It is a prvalue of type std::nullptr_t. There exist implicit conversions from nullptr to null pointer value of any pointer type and any pointer to member type.