标签: nullptr

使用Qt Creator C++ 11,nullptr是关键的?

我正在使用Qt Creator使用C++ 11.

"warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat]"
"error: 'nullptr' was not declared in this scope"
Run Code Online (Sandbox Code Playgroud)

这是在其他地方工作的代码,相关部分是:

... = nullptr;
Run Code Online (Sandbox Code Playgroud)

可能是什么问题?

这不是一个关键字,它的范围不是全球性的吗?

c++ qt-creator nullptr c++11

13
推荐指数
1
解决办法
2万
查看次数

应该在nullptr赋值上使用std :: move吗?

我遇到了以下情况.在nullptr上进行移动有什么好处吗?我假设它基本上为Node*分配一个零,所以我不确定在这里做一个移动是否有任何好处.有什么想法吗?

template <typename T>
struct Node
{
  Node(const T& t): data(t), next(std::move(nullptr)) { }
  Node(T&& t): data(std::move(t)), next(std::move(nullptr)) { }

  T data;
  Node* next;
};
Run Code Online (Sandbox Code Playgroud)

nullptr move-semantics c++11

13
推荐指数
1
解决办法
5855
查看次数

我可以针对null检查C++迭代器吗?

我在使用矢量迭代器时遇到了麻烦.我在一些地方读过,检查null迭代器是不可能的,检查迭代器的常用方法是在搜索后检查vector.end().例如:

vector< Animal* > animalList;

vector<Animal*>::iterator findInList(const type_info& type)
{
    // Loop through list of Animals, if Dog found, return iterator to it
}

auto it = findInList(typeid(Dog));
// With a pointer I can check if it's null, but with an iterator I have to check against animalList.end();
Run Code Online (Sandbox Code Playgroud)

问题是容器可能是空的.使用迭代器,我不能返回null以指示容器为空或搜索失败.我可以返回vector :: end(),但cplusplus.com说:

If the container is empty, vector::end() function returns the same as vector::begin()
Run Code Online (Sandbox Code Playgroud)

然后对于vector :: begin()它说:

If the container is empty, the returned iterator value shall not be dereferenced.
Run Code Online (Sandbox Code Playgroud)

所以,如果我有一个空容器,vector …

c++ pointers iterator vector nullptr

12
推荐指数
1
解决办法
2万
查看次数

检查C++中是否只有一个字符串变量不是nullptr

我有三个LPCWSTR字符串变量叫A,B,C.

我从另一个函数中分配它们,nullptr如果出现问题,有时可以返回.像这样:

A = MyFunc();
B = MyFunc();
C = MyFunc();
Run Code Online (Sandbox Code Playgroud)

现在,对于那些带有这些变量的东西,我需要检查这些变量中是否只有一个变量nullptr(只分配了一个变量).

我自己尝试这样做:

if ((A == nullptr) && (B == nullptr) && (C <> nullptr)) {}
Run Code Online (Sandbox Code Playgroud)

有关如何做到这一点的任何想法都是受欢迎的

c++ variables null nullptr

12
推荐指数
3
解决办法
1856
查看次数

强类型的nullptr?

我刚读了一篇关于C++ 0x标准的文章:http://www.softwarequalityconnection.com/2011/06/the-biggest-changes-in-c11-and-why-you-should-care/

它表示nullptr强类型,这意味着它可以与整数0区分开来.

f(int);
f(char* p);

f(nullptr); // calls char* version
Run Code Online (Sandbox Code Playgroud)

这一切都很好,但我很有兴趣知道标准说nullptr两个指针功能:

f(char* p);
f(int* p);

f(nullptr);
Run Code Online (Sandbox Code Playgroud)

我需要演员吗?是nullptr模板化的吗?

f((int*)(nullptr);
f(static_cast<int*>(nullptr));
f(nullptr<int>);               // I would prefer this
Run Code Online (Sandbox Code Playgroud)

types nullptr c++11

11
推荐指数
1
解决办法
859
查看次数

使用nullptr_t时,为什么会出现"参数设置但未使用"警告?

我有一个自定义类实现operator==nullptr.

这是我的代码愚蠢到一个简单的例子:

#include <cstdint>
#include <iostream>

class C {
private:
    void *v = nullptr;

public:
    explicit C(void *ptr) : v(ptr) { }

    bool operator==(std::nullptr_t n) const {
        return this->v == n;
    }
};

int main()
{
    uint32_t x = 0;
    C c(&x);
    std::cout << (c == nullptr ? "yes" : "no") << std::endl;

    C c2(nullptr);
    std::cout << (c2 == nullptr ? "yes" : "no") << std::endl;


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

代码按预期工作,但g ++(版本6.2.1)给出了以下警告:

[Timur@Timur-Zenbook misc]$ g++ aaa.cpp -o …
Run Code Online (Sandbox Code Playgroud)

c++ warnings g++ nullptr c++11

11
推荐指数
1
解决办法
2299
查看次数

如何正确使用nullptr?

目前我正在阅读Byarne Stroustrup的"C++之旅".重要的是:关于"指针,数组和引用",他举了一个关于如下使用的例子nullptr:

int count_x(char* p, char x)
// count the number of occurrences of x in p[]
// p is assumed to point to a zero-terminated array of char (or to nothing)
{

    if (p == nullptr) return 0;
        int count = 0;

    for (; p != nullptr; ++p)
        if (*p == x)
            ++count;

    return count;
}
Run Code Online (Sandbox Code Playgroud)

在我的主要内容中:

int main(){

    char* str = "Good morning!";
    char c = 'o';
    std::cout << count_x(str, c) << std::endl;

    return 0;
} …
Run Code Online (Sandbox Code Playgroud)

c++ nullptr

11
推荐指数
1
解决办法
1234
查看次数

为什么我可以在不包含STL的情况下使用nullptr?

C++ nullptr的类型为std :: nullptr_t.

为什么一个程序喜欢

int main() {
 int* ptr = nullptr;
}
Run Code Online (Sandbox Code Playgroud)

仍然有效,虽然它不包括任何STL库?

c++ pointers stl nullptr c++11

10
推荐指数
1
解决办法
603
查看次数

nullptr_t 是否会破坏类型双关或指针转换?

考虑这个联盟:

\n
typedef union\n{\n  void*      vptr;\n  nullptr_t  nptr;\n} pun_intended;\n
Run Code Online (Sandbox Code Playgroud)\n

nullptr_t据说与void* 1)兼容。好吧,如果我们将 初始化void*为某个非零值怎么办?

\n
pun_intended foo = { .vptr = (void*)42 }; \n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  • 根据 C23 6.3.2.3 \xc2\xa74,此转换被认为是合法的(impl.defined),或者至少在nullptr_t引入之前是合法的。
  • \n
  • 那么联合类型双关语又如何呢?据说也是合法的。
  • \n
  • 以及使用字符类型指针检查 C 中任何类型的内部表示,该指针在 C23, 6.3.2.3 \xc2\xa77 之前都是明确定义的。
  • \n
\n

完整示例:

\n
#include <stdio.h>\n#include <inttypes.h>\n#include <stddef.h>\n\ntypedef union\n{\n  void*      vptr;\n  nullptr_t  nptr;    \n} pun_intended;\n\nint main(void)\n{\n  pun_intended foo = { .vptr = (void*)42 };\n  printf("Value: %" PRIuPTR "\\n", (uintptr_t)foo.vptr);\n\n  if(foo.nptr != (void*)42)\n  {\n    puts("It …
Run Code Online (Sandbox Code Playgroud)

c nullptr language-lawyer type-punning c23

10
推荐指数
1
解决办法
259
查看次数

将 nullptr 分配给成员指针

谁能告诉我为什么对于 MSVC、clang 和 g++,nullptr分配给类型数据成员指针的内部表示是 -1?Class::*对于 64 位系统来说(size_t)1 << 63是最好的,因为如果您nullptr以这种方式使用成员指针,您肯定会接触内核内存并发生崩溃,因此这将是一个很好的调试辅助工具。

-1背后有更深层次的原因吗?

样本:

struct X
{
    int x, y;
};

using member_ptr = int X::*;

member_ptr f()
{
    return nullptr;
}
Run Code Online (Sandbox Code Playgroud)

...使用 g++ 生成以下二进制文件:

movq    $-1, %rax
ret
Run Code Online (Sandbox Code Playgroud)

c++ pointer-to-member nullptr

9
推荐指数
1
解决办法
501
查看次数