【发布时间】:2019-10-11 12:41:57
【问题描述】:
我有以下代码,其中 std::tuple 适用于 nullptr 但不适用于 NULL。
#include <iostream>
#include <tuple>
int main()
{
tuple<int*> t1, t2;
t1 = std::make_tuple(NULL);
t2 = std::make_tuple(nullptr);
}
使用 C++11 编译时,代码在使用 nullptr 时有效,但在使用 NULL 时会出现以下错误。
In file included from tuple.cpp:2:
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/tuple:447:8: error: assigning to 'int *' from incompatible type 'long'
= std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/tuple:575:36: note: in instantiation of function template specialization 'std::_Tuple_impl<0, int *>::operator=<long>' requested here
static_cast<_Inherited&>(*this) = std::move(__in);
^
tuple.cpp:13:8: note: in instantiation of function template specialization 'std::tuple<int *>::operator=<long, void>' requested here
t1 = std::make_tuple(NULL);
^
1 error generated.
这里,NULL的类型是long int,并且元组足够严格,不接受。
我们怎样才能使它也使用 NULL 工作,因为我们的客户说它在与 nvcc 编译器一起使用时可以工作(当他们在 CUDA 代码中使用上述 sn-p 时),但它不能工作。
【问题讨论】:
-
简单的反应是停止使用
NULL。nullptr在每个地方都可以工作NULL在没有问题的地方都可以工作,因为你没有正确使用NULL,所以应该修复它。 -
@NathanOliver 谢谢。但是如果他们仍然想使用NULL,有没有办法支持。我知道将其类型转换为 int* 是可行的,但我想知道 nvcc 编译器如何处理这种情况,因为他们说与 nvcc 一起使用时不会发生此错误。
-
NVCC 可以像
(void*)0一样定义NULL,这可以解释它为什么起作用。它不是合法的 C++,但没有实现是 100% 合法的。 -
谢谢。一定是在nvcc中定义NULL的方式与使用的编译器不同。