我正在使用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)
可能是什么问题?
这不是一个关键字,它的范围不是全球性的吗?
我遇到了以下情况.在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) 我在使用矢量迭代器时遇到了麻烦.我在一些地方读过,检查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 …
我有三个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++ 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) 我有一个自定义类实现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) 目前我正在阅读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的类型为std :: nullptr_t.
为什么一个程序喜欢
int main() {
int* ptr = nullptr;
}
Run Code Online (Sandbox Code Playgroud)
仍然有效,虽然它不包括任何STL库?
考虑这个联盟:
\ntypedef union\n{\n void* vptr;\n nullptr_t nptr;\n} pun_intended;\nRun Code Online (Sandbox Code Playgroud)\nnullptr_t据说与void* 1)兼容。好吧,如果我们将 初始化void*为某个非零值怎么办?
pun_intended foo = { .vptr = (void*)42 }; \nRun Code Online (Sandbox Code Playgroud)\nnullptr_t引入之前是合法的。完整示例:
\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) 谁能告诉我为什么对于 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)