我有以下代码,其中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代码中使用上述片段),它可以工作,但否则不起作用。
NULL
。nullptr
可以在每个NULL
可以使用的地方使用,并且在不能使用NULL
的地方,你应该修复代码,因为你没有正确使用NULL
。 - NathanOliver(void*)0
一样定义NULL
,这就解释了为什么它能工作。虽然这不是合法的C++,但没有任何实现是100%合法的。 - NathanOliver