可能重复:
nullptr究竟是什么?
我首先认为这是一个关键字.我目前的gcc没有nullptr以不同的色调突出显示.为了验证这一点,我写了以下内容:
void *&p = nullptr;
Run Code Online (Sandbox Code Playgroud)
所以我从错误中得到了一些线索:
错误:从'std :: nullptr_t'类型的右值开始,无效初始化'void*&'类型的非const引用
如果nullptr是一个对象那么它真的是一个简单的指针0吗?换句话说,假设我写道:
#define NULL nullptr
Run Code Online (Sandbox Code Playgroud)
以上语句是否不会改变我的代码中的任何内容?此外,了解std::nullptr_t类型的其他用例会很有趣.
非空原始指针的操作nullptr < ptr和ptr < nullptr定义是否良好ptr != nullptr?欢迎引用 C++ 标准。
我有一些overloaed方法,它们采用了一些不同的指针类型.
现在我想将一个特定方法nullptr作为参数调用.
我知道我可以将其nullptr转换为特定类型的指针,我想要它调用的方法.
但我不想/不能投nullptr.
这个例子应该解释我想要做的事情:
class Foo {
//some attributes
};
class Bar {
//some attributes
};
void myMethod (Foo*) {
//I want this method to be called
}
void myMethod (Bar*) {
//Not this one
}
int main () {
myMethod(nullptr); //Something like this
// myMethod(static_cast<nullptr>); //I don't want to write this.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我只是用nullptr我得到它来调用它,
error: call of overloaded 'myMethod(std::nullptr_t)' is ambiguous
因为编译器不知道它应该调用哪个方法.
有办法做我想要的吗?
喜欢类似于模板专业化的东西?
当实现我自己的unique_ptr(只是为了好玩),我发现它无法通过这个测试文件来自libstdcxx:
struct A;
struct B
{
std::unique_ptr<A> a;
};
struct A
{
B* b;
~A() { VERIFY(b->a != nullptr); }
};
void test01()
{
B b;
b.a.reset(new A);
b.a->b = &b;
}
Run Code Online (Sandbox Code Playgroud)
gcc愉快地传递了这个测试文件(当然,这个文件是来自libstdcxx),而clang因为VERIFY部分失败了.
题:
b->a != nullptr)对于gcc很重要,否则它没有测试文件,但我不知道它背后是什么.它与优化有关吗?我知道很多UB都是为了更好的优化.以下代码愚弄nullptr指针和引用:
#include <cstdio>
void printRefAddr(int &ref) {
printf("printAddr %p\n", &ref);
}
int main() {
int *ip = nullptr;
int &ir = *ip;
// 1. get address of nullptr reference
printf("ip=%p &ir=%p\n", ip, &ir);
// 2. dereference a nullptr pointer and pass it as reference
printRefAddr(*ip);
// 3. pass nullptr reference
printRefAddr(ir);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题:在C++标准中,是注释语句1..3有效代码还是未定义的行为?
C++的不同版本是相同还是不同(旧版本当然会使用0文字而不是nullptr关键字)?
奖金问题:是否有已知的编译器/优化选项,这实际上会导致上面的代码做出意外/崩溃的事情?例如,是否有任何编译器的标志,它将nullptr为初始化引用的所有地方生成隐式断言,包括从*ptr?传递引用参数?
好奇的示例输出,没什么意外:
ip=(nil) &ir=(nil)
printAddr (nil)
printAddr (nil)
Run Code Online (Sandbox Code Playgroud) 在Visual Studio 2012中,我正在搞乱指针,我意识到这个程序一直在崩溃:
#include <iostream>
using std::cout;
using std::endl;
int main ()
{
const char* pointer = nullptr;
cout << "This is the value of pointer " << pointer << "." << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的意图是在设置pointer到null,然后打印地址.即使程序编译,它也会在运行时崩溃.有人可以解释发生了什么吗?
另外,pointer和的区别是什么 *pointer
在我班上的方法中,我正在检查值是否为0才能返回nullptr,但我似乎无法做到这一点.
Complex Complex::sqrt(const Complex& cmplx) {
if(cmplx._imag == 0)
return nullptr;
return Complex();
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是: could not convert 'nullptr' from 'std::nullptr_t' to 'Complex'
我现在意识到,这nullptr是指针,但是,我的对象不是指针,有没有办法让我将它设置为null或类似的东西?
我在C++中编码,并使用在失败的情况下返回NULL的C函数.什么是正确的想法,将其返回值与NULL或nullptr进行比较?
if ((CreateEventEx(myEventHandleHere) == NULL)
{
...
}
Run Code Online (Sandbox Code Playgroud)
要么
if ((CreateEventEx(myEventHandleHere) == nullptr)
{
...
}
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
#include <iostream>
using namespace std;
void fun(const char* s){
if (s == nullptr) {
puts("const char* nullptr");
} else {
printf("%s\n", s);
}
}
template <typename T>
void fun(T* p){
printf("%p\n", p);
}
int main() {
int a;
fun("abc"); // Resolves to fun(const char*)
fun(&a); // Specializes the template to int*
fun(nullptr); // Uses fun(const char*)??
fun(NULL); // Same as above
}
Run Code Online (Sandbox Code Playgroud)
我很诧异g++ 7.2.0也不会抛出不明的重载解析错误,因为我认为nullptr并NULL可以融入任何指针类型,其中包括fun(int*)专门从模板,只要不是专门用于过载std::nullptr_t.
为什么 …
#include <memory>
void f1(std::shared_ptr<bool> ptr) {}
int main() {
f1(0); // OK
f1(1); // compilation error: could not convert ‘1’ from ‘int’ to ‘std::shared_ptr<bool>’
}
Run Code Online (Sandbox Code Playgroud)
既为int,为什么0但1可以转换为std::shared_ptr<T>?
如何从转换的残疾1来std::shared_ptr<T>编译时进行检查?
如何从转换的残疾1来std::nullptr_t编译时进行检查?
c++ ×10
nullptr ×10
c++11 ×3
null ×3
pointers ×3
overloading ×1
reference ×1
shared-ptr ×1
standards ×1
unique-ptr ×1