它似乎nullptr是在默认的全局命名空间中声明的.它在std命名空间中是否有意义?
好的,首先是一些可能相关的事情:
我在C++ 11模式下使用Clang 3.1编译器,标准库设置为libc ++.
我正在尝试熟悉C++ 11,并且这样做我遇到的行为似乎很奇怪.它可能是Clang或libc ++的怪癖,但我不能说C++标准,我无法访问其他支持C++ 11的编译器,所以我无法检查它,我搜索了互联网和Stack Overflow在没有发现任何相关的情况下尽我所能......所以我们走了:
当使用shared_ptr/unique_ptr为简单资源实现RAII时,它们的行为似乎与删除时的空指针不同.我意识到通常没有必要删除空指针,但我原本期望这两个STL智能指针之间的行为至少匹配.
对于特定情况,请考虑以下代码:
{
auto Deleter = [](void *){cout << "It's later!" << endl;};
shared_ptr<void> spDoSomethingLater(nullptr, Deleter);
unique_ptr<void, void (*)(void *)> upDoSomethingLater(nullptr, Deleter);
cout << "It's now!" << endl;
}
Run Code Online (Sandbox Code Playgroud)
我原本期望得到以下输出之一:
a)如果两个删除器都被调用,即使指针为空:
"It's now!"
"It's later!"
"It's later!"
Run Code Online (Sandbox Code Playgroud)
b)如果由于指针为空而未调用任何删除器:
"It's now!"
Run Code Online (Sandbox Code Playgroud)
但我没有观察到这些情况.相反,我观察到:
"It's now!"
"It's later!"
Run Code Online (Sandbox Code Playgroud)
这意味着正在调用一个但不是另一个删除者.经过进一步研究,我发现无论是否包含null值,都会调用shared_ptr的删除器,但只有在不包含空值的情况下才会调用unique_ptr的删除器.
我的问题:这实际上是标准规定的正确行为吗?如果是这样,为什么两种STL类型之间的指定行为会以这种方式不同?如果没有,这是一个我应该向libc ++报告的错误吗?
这个问题基于我发现监控可能内存泄漏的代码,因此它包含一些您可能不希望在常规程序中看到的代码,例如排序指针。
但是,我看到设置了一个指针nullptr,然后将该指针与最大地址进行比较。是否由 C++ 标准保证nullptr总是小于其他指针operator<?
最近尝试了以下程序,它编译,运行正常并产生预期的输出,而不是任何运行时错误.
#include <iostream>
class demo
{
public:
static void fun()
{
std::cout<<"fun() is called\n";
}
static int a;
};
int demo::a=9;
int main()
{
demo* d=nullptr;
d->fun();
std::cout<<d->a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果未初始化的指针用于访问类和/或结构成员,则行为未定义,但为什么允许使用空指针访问静态成员.我的计划有什么危害吗?
我正在玩SFINAE并发现我无法解释的行为.
这编译正常:
template<typename Integer,
std::enable_if_t<std::is_integral<Integer>::value>* = nullptr>
void foo(Integer) {}
template<typename Floating,
std::enable_if_t<std::is_floating_point<Floating>::value>* = nullptr>
void foo(Floating) {}
Run Code Online (Sandbox Code Playgroud)
虽然这个(nullptr替换为0):
template<typename Integer,
std::enable_if_t<std::is_integral<Integer>::value>* = 0>
void foo(Integer) {}
template<typename Floating,
std::enable_if_t<std::is_floating_point<Floating>::value>* = 0>
void foo(Floating) {}
Run Code Online (Sandbox Code Playgroud)
prog.cpp: In function ‘int main()’: prog.cpp:13:10: error: no matching function for call to ‘foo(int)’
foo(3);
^ prog.cpp:5:6: note: candidate: template<class Integer, std::enable_if_t<std::is_integral<_Tp>::value>* <anonymous> > void foo(Integer) void foo(Integer) {}
^~~ prog.cpp:5:6: note: template argument deduction/substitution failed: prog.cpp:4:64: …Run Code Online (Sandbox Code Playgroud) 我正在使用g ++ 4.4.1并希望使用nullptr,但我无法找到需要包含哪个头文件.它似乎也不是关键字,因为我尝试使用它被拒绝了
error: 'nullptr' was not declared in this scope
Run Code Online (Sandbox Code Playgroud) 这段代码片段有效吗?:
unique_ptr<A> p(new A());
p = nullptr;
Run Code Online (Sandbox Code Playgroud)
也就是说,我可以分配nullptr到unique_ptr吗?或者会失败?
我用g ++编译器尝试了这个并且它有效,但是其他编译器呢?
我可以使用nullptr关键字作为变量函数的参数吗?如果是这样,它是否经历任何类型的标准转换,结果值的类型是什么?
具体来说,以下是正确的吗?
std::printf("%p", nullptr);
Run Code Online (Sandbox Code Playgroud)
或者它必须是:
std::printf("%p", static_cast<void *>(nullptr));
Run Code Online (Sandbox Code Playgroud) C++ 11实现会定义NULL为nullptr吗?
这是由新的C++标准规定的吗?
我正在重构一些NULL在很多地方使用的旧代码.问题是
盲目地更换所有
NULL实例是否安全nullptr?
我特别感兴趣的是,替换NULL by nullptr可能会导致一些运行时错误(编译时错误可以),但我想不出任何错误.如果没有,通过nullptr自动替换NULL是否安全(如果有的话,修复编译时错误).
如果之前已经提出过问题我很抱歉 - 我找不到它,如果你指出我的答案,我会删除它!
nullptr ×10
c++ ×9
c++11 ×7
null ×2
unique-ptr ×2
g++ ×1
pointers ×1
shared-ptr ×1
templates ×1