标签: nullptr

nullptr不是特殊关键字和std :: nullptr_t的对象吗?

可能重复:
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类型的其他用例会很有趣.

c++ pointers nullptr c++11

4
推荐指数
2
解决办法
3499
查看次数

非空原始指针和 nullptr 之间的小于运算符

非空原始指针的操作nullptr < ptrptr < nullptr定义是否良好ptr != nullptr?欢迎引用 C++ 标准。

c++ standards comparison-operators nullptr c++11

4
推荐指数
1
解决办法
393
查看次数

使用nullptr调用重载方法是不明确的

我有一些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
因为编译器不知道它应该调用哪个方法.

有办法做我想要的吗?
喜欢类似于模板专业化的东西?

c++ overloading nullptr

4
推荐指数
2
解决办法
487
查看次数

std :: unique_ptr是否在其析构函数中将其底层指针设置为nullptr?

当实现我自己的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部分失败了.

题:

  1. 它是依赖于实现还是未定义的行为?
  2. 我想这个postcondition(b->a != nullptr)对于gcc很重要,否则它没有测试文件,但我不知道它背后是什么.它与优化有关吗?我知道很多UB都是为了更好的优化.

c++ unique-ptr nullptr

4
推荐指数
1
解决办法
329
查看次数

nullptr是否在C++中引用了未定义的行为?

以下代码愚弄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)

c++ reference undefined-behavior nullptr

3
推荐指数
1
解决办法
3367
查看次数

C++将char指针设置为null

在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)

我的意图是在设置pointernull,然后打印地址.即使程序编译,它也会在运行时崩溃.有人可以解释发生了什么吗?

另外,pointer和的区别是什么 *pointer

c++ pointers nullptr

3
推荐指数
1
解决办法
2125
查看次数

无法将nullptr返回到我的类C++

在我班上的方法中,我正在检查值是否为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 nullptr

3
推荐指数
1
解决办法
4584
查看次数

C++:将C函数的返回值与NULL或nullptr进行比较?

我在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)

c++ null nullptr

3
推荐指数
1
解决办法
2587
查看次数

如果有专门的函数和模板函数,为什么没有必要专门研究`std :: nullptr_t`

请考虑以下代码:

#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不会抛出不明的重载解析错误,因为我认为nullptrNULL可以融入任何指针类型,其中包括fun(int*)专门从模板,只要不是专门用于过载std::nullptr_t.

为什么 …

c++ null nullptr language-lawyer overload-resolution

3
推荐指数
1
解决办法
113
查看次数

为什么我可以将0转换为std :: shared_ptr <T>而不是1?

#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,为什么01可以转换为std::shared_ptr<T>

如何从转换的残疾1std::shared_ptr<T>编译时进行检查?

如何从转换的残疾1std::nullptr_t编译时进行检查?

c++ pointers shared-ptr nullptr c++11

3
推荐指数
2
解决办法
189
查看次数