nullptr

How is possible that accessing nullptr works? [duplicate]

那年仲夏 提交于 2019-12-04 07:08:35
问题 This question already has answers here : Why am I able to make a function call using an invalid class pointer (6 answers) Calling class method through NULL class pointer [duplicate] (10 answers) Closed 3 years ago . I have a simple class: class B { public: int getData() { return 3; } }; then, I initialize a pointer to it with nullptr: B *foo{ nullptr }; And then, trying to use it comes the surprise: int t = foo->getData(); and t is now 3. How is that possible without constructing the class?

Why can't you take the address of nullptr?

浪尽此生 提交于 2019-12-03 00:57:15
In the C++11 standard, I don't understand the reason why taking the address of nullptr is disallowed whereas one is allowed to take the address of their own std::nullptr_t instances. Aside from the fact that nullptr is a reserved keyword, is there any designated reasoning for this decision? Simply because it amuses me, I attempted to circumnavigate this restriction with the following function: decltype(nullptr)* func(const decltype(nullptr) &nref) noexcept { return const_cast<decltype(nullptr)*>(reinterpret_cast<const decltype(nullptr)*>(&nref)); } I had to use reinterpret_cast on the

Is it safe to #define NULL nullptr?

人盡茶涼 提交于 2019-12-02 20:02:15
I have seen below macro in many topmost header files: #define NULL 0 // C++03 In all over the code, NULL and 0 are used interchangeably. If I change it to. #define NULL nullptr // C++11 Will it cause any bad side effect ? I can think of the only (good) side effect as following usage will become ill-formed; int i = NULL; I have seen below macro in topmost header file: You shouldn't have seen that, the standard library defines it in <cstddef> (and <stddef.h> ). And, IIRC, according to the standard, redefining names defined by standard header files results in undefined behaviour. So from a purely

How to define our own nullptr in c++98?

假装没事ソ 提交于 2019-12-02 13:36:31
Actually I am writing my own version of all library classes, and I don't want to include the STL files into my class file. So, for example, I want to check whether the node is equal to null. If I write something like #define nullptr 0 Then it is not working with some other node pointer (i.e. Node *root = nullptr ) How to do that is mentioned in the book: Effective C++, 2nd edition by Scott Meyers (newer edition is available) in chapter: " Item 25: Avoid overloading on a pointer and a numerical type. ". It is needed if your compiler doesn't know the nullptr keyword introduced by C++11. const /*

How is possible that accessing nullptr works? [duplicate]

做~自己de王妃 提交于 2019-12-02 10:24:16
This question already has an answer here: Why am I able to make a function call using an invalid class pointer 6 answers Calling class method through NULL class pointer [duplicate] 10 answers I have a simple class: class B { public: int getData() { return 3; } }; then, I initialize a pointer to it with nullptr: B *foo{ nullptr }; And then, trying to use it comes the surprise: int t = foo->getData(); and t is now 3. How is that possible without constructing the class? Is it because getData() does not use "this"? That broke all my knowledge about pointers. Is that expected behavior? I am working

Assigned `nullptr` to `bool` type. Which compiler is correct?

早过忘川 提交于 2019-12-01 04:00:02
I have a following snippet of code that assigned nullptr to bool type. #include <iostream> int main() { bool b = nullptr; std::cout << b; } In clang 3.8.0 working fine. it's give an output 0 . Clang Demo But g++ 5.4.0 give an error: source_file.cpp: In function ‘int main()’: source_file.cpp:5:18: error: converting to ‘bool’ from ‘std::nullptr_t’ requires direct-initialization [-fpermissive] bool b = nullptr; Which compiler is correct? From the C++ Standard (4.12 Boolean conversions) 1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a

implementation safe nullptr

笑着哭i 提交于 2019-11-30 14:23:44
I'd like to keep my code compilable both on legacy C++ (C++ code using "NULL") and new C++11 standard (C++ code using "nullptr") I'm using GCC, but planning to recompile the whole codebase also for VS when I'll finish most important things. Should I expect both GCC and VS will do something like #define NULL nullptr Or Is better I'll do that myself (using of course a different name, where MY_LIB will be replaced by my library suffix)? #ifndef nullptr #define MY_LIB_NULL NULL #else #define MY_LIB_NULL nullptr #endif What I want to achieve is code that compiles regardless of wich C++11 features

Does the standard behavior for deleters differ between shared_ptr and unique_ptr in the case of null pointers?

谁都会走 提交于 2019-11-30 10:49:53
OK, so first some things that might be relevant: I'm using the Clang 3.1 compiler, in C++11 mode, with the standard library set to libc++. I'm trying to familiarize myself with C++11, and in so doing I ran across behavior that seems odd. It may be a quirk of Clang or libc++ but I can't speak C++ standardese and I have no access to other compilers with C++11 support so I can't really check it, and I've searched the internet and Stack Overflow to the best of my ability without finding anything related...so here we go: When using shared_ptr / unique_ptr to implement RAII for a simple resource, it

Pure virtual functions in C++11

天涯浪子 提交于 2019-11-30 07:53:08
In C++98, the null pointer was represented by the literal 0 (or in fact any constant expression whose value was zero). In C++11, we prefer nullptr instead. But this doesn't work for pure virtual functions: struct X { virtual void foo() = nullptr; }; Why does this not work? Would it not make total sense? Is this simply an oversight? Will it be fixed? Because the syntax says 0 , not expression or some other non-terminal matching nullptr . For all the time only 0 has worked. Even 0L would be ill-formed because it does not match the syntax. Edit Clang allows = 0x0 , = 0b0 and = 00 (31.12.2013).

implementation safe nullptr

て烟熏妆下的殇ゞ 提交于 2019-11-29 20:58:49
问题 I'd like to keep my code compilable both on legacy C++ (C++ code using "NULL") and new C++11 standard (C++ code using "nullptr") I'm using GCC, but planning to recompile the whole codebase also for VS when I'll finish most important things. Should I expect both GCC and VS will do something like #define NULL nullptr Or Is better I'll do that myself (using of course a different name, where MY_LIB will be replaced by my library suffix)? #ifndef nullptr #define MY_LIB_NULL NULL #else #define MY