nullptr

Pure virtual functions in C++11

╄→гoц情女王★ 提交于 2019-11-29 10:37:58
问题 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? 回答1: 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

In c++11, does dynamic_cast return nullptr or 0?

邮差的信 提交于 2019-11-29 07:19:40
I want to check the result of dynamic_cast. In c++11 (or c++0x, for compilers that support nullptr), should I compare against nullptr or 0? Does it matter, and if so, why? Is the result compiler-dependent? Both the constant nullptr (which is of type nullptr_t ) and the constant 0 will implicitly convert to the null value of any pointer type. So comparing against either one will work and is technically OK. By the way, this means that dynamic_cast return neither one, it returns the null value for the particular pointer type. It's probably best to get in the habit of using nullptr rather than 0 .

What is the type of nullptr?

南笙酒味 提交于 2019-11-28 12:16:37
The Standard states, that nullptr is a pointer literal of type std::nullptr_t (2.14.7). And 18.2p9 defines nullptr_t by namespace std { typedef decltype(nullptr) nullptr_t; } By 7.1.6.2p4 decltype(nullptr) is the type of the expression nullptr , which is by definition std::nullptr_t (since the expression nullptr is a prvalue). Substituting that into the definition of nullptr_t results in typedef nullptr_t nullptr_t On the other hand a typedef specifier does not introduce a new type, it's just a name for another existing type. So, what is exactly nullptr_t ? I'm not able to comprehend these

What are the uses of the type `std::nullptr_t`?

感情迁移 提交于 2019-11-28 11:55:44
I learned that nullptr , in addition to being convertible to any pointer type (but not to any integral type) also has its own type std::nullptr_t . So it is possible to have a method overload that accepts std::nullptr_t . Exactly why is such an overload required? If more than one overload accepts a pointer type, an overload for std::nullptr_t is necessary to accept a nullptr argument. Without the std::nullptr_t overload, it would be ambiguous which pointer overload should be selected when passed nullptr . Example: void f(int *intp) { // Passed an int pointer } void f(char *charp) { // Passed a

Are these null pointers, or are they pointers to address 0?

拜拜、爱过 提交于 2019-11-28 09:04:37
If I write int zero = 0; void *p1 = (void *)0; void *p2 = (void *)(int)0; void *p3 = (void *)(0 /*no-op, but does it affect the next zero?*/, 0); void *p4 = (void *)zero; // For reference, this is a pointer to address zero void *p5 = 0; // For reference, this is a null pointer void *p6 = NULL; // For reference, this is a null pointer void *p7 = nullptr; // For reference, this is a null pointer (C++11) static const int static_zero_1 = 0; // Is this a literal zero when used? static const int static_zero_2 = 1 - 1; // No "literals 0" per se... is it? void *p8 = (void *)static_zero_1; // I have

gcc nullptr issue

青春壹個敷衍的年華 提交于 2019-11-28 07:55:48
问题 I am porting existing code to compile under gcc 4.7.2 and have run into a strange issue with nullptr. I have managed to boil it down to a simple test case: #include <stdio.h> const char* g_marker = "Original value"; void SetMarker( const char* s ) { g_marker = s; } char* Test1() { return SetMarker( "I was here 1" ), nullptr; } char* Test2() { SetMarker( "I was here 2" ); return nullptr; } char* Test3() { return SetMarker( "I was here 3"), (char*)NULL; } int main() { char* returnValue = Test1(

In c++11, does dynamic_cast return nullptr or 0?

∥☆過路亽.° 提交于 2019-11-27 18:36:13
问题 I want to check the result of dynamic_cast. In c++11 (or c++0x, for compilers that support nullptr), should I compare against nullptr or 0? Does it matter, and if so, why? Is the result compiler-dependent? 回答1: Both the constant nullptr (which is of type nullptr_t ) and the constant 0 will implicitly convert to the null value of any pointer type. So comparing against either one will work and is technically OK. By the way, this means that dynamic_cast return neither one, it returns the null

“Backporting” nullptr to C++-pre-C++0x programs

删除回忆录丶 提交于 2019-11-27 16:25:44
问题 More or less what the title suggests. While I'm not yet using C++0x I'd like to be prepared for when it happens, and I'd also like to reduce the amount of code I have to rewrite to use some of its facilities. That way I can get backwards and forwards compatibility in one go. One of the most interesting ones I have found is nullptr, which I've been using more often recently. After checking the "Official workaround" and Meyer's suggestion, I decided that I'd like to use this in both my C++ and

Can nullptr be emulated in gcc?

馋奶兔 提交于 2019-11-27 11:12:31
I saw that nullptr was implemented in Visual Studio 2010. I like the concept and want to start using it as soon as possible; however GCC does not support it yet. My code needs to run on both (but doesn't have to compile with other compilers). Is there a way to "emulate" it? Something like: #define nullptr NULL (That obviously wouldn't work well at all, it's just to show what I mean.) The Official proposal has a workaround - const // this is a const object... class { public: template<class T> // convertible to any type operator T*() const // of null non-member { return 0; } // pointer...

What is the type of nullptr?

最后都变了- 提交于 2019-11-27 06:01:21
问题 The Standard states, that nullptr is a pointer literal of type std::nullptr_t (2.14.7). And 18.2p9 defines nullptr_t by namespace std { typedef decltype(nullptr) nullptr_t; } By 7.1.6.2p4 decltype(nullptr) is the type of the expression nullptr , which is by definition std::nullptr_t (since the expression nullptr is a prvalue). Substituting that into the definition of nullptr_t results in typedef nullptr_t nullptr_t On the other hand a typedef specifier does not introduce a new type, it's just