nullptr

What C++0x Headers are supposed to define nullptr?

久未见 提交于 2019-12-12 07:54:01
问题 Now that C++0x is almost here, I've been experimenting with it, and in particular using nullptr. I haven't been able to figure out what standard header files one is supposed to include if one needs to use it. Any help is appreciated. 回答1: No headers should be required. It is a built-in keyword (§[lex.nullptr]). 2.14.7 Pointer literals [lex.nullptr] pointer-literal: nullptr The pointer literal is the keyword nullptr . It is a prvalue of type std::nullptr_t . [ Note: std::nullptr_t is a

Validating a pointer to a pointer in C++

独自空忆成欢 提交于 2019-12-11 16:59:06
问题 I am trying to write a function that receives a pointer, uses it, and then makes it point to a new object. In order to do this, I am using a ptr-to-ptr. This is how I validate the ptr-to-ptr received by my function: void modifyPtr(Obj ** ptrToPtr) { if (*ptrToPtr == nullptr) { return; } else { // Do everything else! } } While writing this, I thought: what if a client passes the following to my function? Obj ** ptrToPtr = nullptr; modifyPtr(ptrToPtr); In that case, my validation will be

Pc Lint, how to suppress err 613(Possible use of null ponter) for class with init()

空扰寡人 提交于 2019-12-11 09:13:33
问题 Tried to simplify the situation as much as possible. So I have a class: class C { int * field; public: C() : field(nullptr) {} void init(int* f) { field = f; } int getI1() { return *field; } int getI2() { return *field; } }; which generates 2 Lint warnings 613 (Possible use of null pointer 'C::i'...) I know that "field" won't be null when getI1() or getI2() are called. And unfortunately I cannot initialize it in constructor. So I want to suppress Lint warnings. I can do it like this class C {

What C++17 standard say about calling delete on nullptr?

牧云@^-^@ 提交于 2019-12-11 02:25:40
问题 C++03 Standard say's: 5.3.5 Delete [...] In either alternative, if the value of the operand of delete is the null pointer the operation has no effect.[...] char *p = nullptr; delete p; //no effect It means, it is valid to delete null pointer in c++. What C++17 standard say about calling delete on nullptr pointer? 回答1: Yes it is valid, and it results in a noop. reference If expression evaluates to a null pointer value, no destructors are called, and the deallocation function is not called. 回答2

C/C++ nullptr dereference [duplicate]

别等时光非礼了梦想. 提交于 2019-12-11 02:04:07
问题 This question already has answers here : Is there a platform or situation where dereferencing (but not using) a null pointer to make a null reference will behave badly? (6 answers) Closed 3 years ago . Since de-referencing nullptr ( NULL ) is an undefined behavior both in C and C++ , I am wondering if expression &(*ptr) is a valid one if ptr is nullptr ( NULL ). If it is also an undefined behavior, how does OFFSETOF macro in the linked answer work? I always thought that ptr->field is a

Perfectly emulate nullptr

会有一股神秘感。 提交于 2019-12-10 14:52:07
问题 I got tired of waiting for compiler support of nullptr (gcc 4.6 does but it's so new few distributions support it). So as a stop gap until nullptr is fully supported I decided to emulate it. There are two examples of emulation: one from here, and one from wikibooks. Of note, neither implementation mentions an operator == . However, without one, the following code will not compile. int* ptr = nullptr; assert( ptr == nullptr ); // error here: missing operator == Is this operator == error a

Is nullptr_t a default constructible type?

十年热恋 提交于 2019-12-09 08:02:24
问题 I can't tell from the C++11 Standard if nullptr_t has a default constructor. In other words, is the following valid?: nullptr_t n; GCC and VC++ allow the above code, but clang does not. I can't find anything in the Standard specifying that it doesn't have a default constructor, and what I can find suggests that it ought to. This matters to me because I'm writing a basic fallback implementation of nullptr for older compiler support and need to know if I need to give it a default constructor.

Is nullptr not a special keyword and an object of std::nullptr_t? [duplicate]

大城市里の小女人 提交于 2019-12-07 07:55:11
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What exactly is nullptr? I first thought it's a keyword. My present gcc doesn't highlight nullptr in a different shade. To verify that, I wrote following: void *&p = nullptr; So I got some clue from the error that: error: invalid initialization of non-const reference of type ‘void*&’ from an rvalue of type ‘std::nullptr_t’ If nullptr is an object then is it really a pointer equivalent of simple 0 ? In other word

How to define nullptr for supporting both C++03 and C++11? [duplicate]

假装没事ソ 提交于 2019-12-07 00:41:06
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: “Backporting” nullptr to C++-pre-C++0x programs How to define nullptr for supporting both C++03 and C++11? Does below code is compiled with both C++03 and C++11 compiles without change the meaning of nullptr in C++11 compiler? #include <cstddef> #if !defined(nullptr) #define nullptr NULL #endif 回答1: In C++11, nullptr is of type nullptr_t . One of its big advantages compared to NULL (or to 0 would say Bjarne

Why can I use nullptr without including STL?

与世无争的帅哥 提交于 2019-12-06 20:24:58
问题 The C++ nullptr is of the type std::nullptr_t . Why does a program like int main() { int* ptr = nullptr; } still work, although it doesn't include any STL library? 回答1: In C++11 they wanted to add a keyword to replace the macro NULL (which is basically defined as #define NULL 0 ), both because it is a core concept, and because of some annoying bugs you get when you are forced to use 0 as your null pointer constant. A number of keywords where proposed. Large codebases where searched to ensure