gsa*_*ras 3 c++ templates nullptr perfect-forwarding c++11
我想运行的功能:
struct foo;
void bar(const foo* p = 0);
Run Code Online (Sandbox Code Playgroud)
我如何调用函数:
auto thread = std::thread(&bar, NULL);
Run Code Online (Sandbox Code Playgroud)
警告:
foobar.h:223:9: 警告:将 NULL 传递给 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (*)(const foo*), _Args 的非指针参数 2 = {int}]' [-Wconversion-null]
我在这里缺少什么?
当我用非NULL参数调用函数时,警告消失了。
问题是这NULL有点模棱两可。
虽然它在语义上是一个指针,但它可以(并且在您的实现中是)整数类型。
18.2 类型
[support.types]3 该宏
NULL是本国际标准 (4.10) 中实现定义的 C++空指针常量。
4.10 指针转换
[conv.ptr]1空指针常量是具有零值或类型为纯右值的整数文字 (2.14.2)
std::nullptr_t。
[...]
因此,您的实现决定使其0向后兼容,但将其标记为额外的诊断。
这实际上是促进可移植代码的一个值得称赞的决定。
虽然很遗憾没有人可以跳进时间机器并NULL与nullptr标准相同,但不存在歧义。
要解决该错误,请使用正确类型的指针nullptr代替NULLor(更复杂且不太好)。
| 归档时间: |
|
| 查看次数: |
9626 次 |
| 最近记录: |