nullptr

Why `void* = 0` and `void* = nullptr` makes the difference?

試著忘記壹切 提交于 2019-12-06 16:49:23
问题 I was playing with SFINAE and found behavior I cannot explain. This compiles fine: 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) {} While this ( nullptr replaced with 0 ): template<typename Integer, std::enable_if_t<std::is_integral<Integer>::value>* = 0> void foo(Integer) {} template<typename Floating, std:

Are nullptr references undefined behaviour in C++? [duplicate]

两盒软妹~` 提交于 2019-12-06 10:26:19
问题 This question already has answers here : Assigning a reference by dereferencing a NULL pointer (5 answers) Closed 6 years ago . The following code fools around with nullptr pointer and reference: #include <cstdio> void printRefAddr(int &ref) { printf("printAddr %p\n", &ref); } int main() { int *ip = nullptr; int &ir = *ip; // 1. get address of nullptr reference printf("ip=%p &ir=%p\n", ip, &ir); // 2. dereference a nullptr pointer and pass it as reference printRefAddr(*ip); // 3. pass nullptr

Compile error 'nullptr' undeclared identifier

亡梦爱人 提交于 2019-12-06 01:54:06
问题 I'm tring to compile a source with Visual Studio 2008 Express but I'm getting this error: Error C2065: 'nullptr' undeclared identifier. My code: if (Data == nullptr) { show("Data is null"); return 0; } I read on google that I should upgrade to Visual Studio 2010, but I don't want to do this because of the 2008 intelisense. Can this be repaired or replaced? 回答1: The error you are getting is because the compiler doesn't recognize the nullptr keyword. This is because nullptr was introduced in a

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

 ̄綄美尐妖づ 提交于 2019-12-05 16:39:27
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, suppose I write: #define NULL nullptr Is the above statement doesn't alter anything in my code ? Also

In function call, why doesn't nullptr match a pointer to a template object?

◇◆丶佛笑我妖孽 提交于 2019-12-05 03:38:52
Here is an example of a code that works perfectly: #include<iostream> #include<vector> template< class D, template< class D, class A > class C, class A = std::allocator< D > > void foo( C< D, A > *bar, C< D, A > *bas ) { std::cout << "Ok!" << std::endl; } int main( ) { std::vector< int > *sample1 = nullptr; std::vector< int > *sample2 = nullptr; foo( sample1, sample2 ); return( 0 ); } In the code below, however, the compiler is unable to match std::vector< int >* with nullptr for the second parameter, even being able to deduct the template types from the first parameter. #include<iostream>

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

僤鯓⒐⒋嵵緔 提交于 2019-12-05 03:14:20
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 In C++11, nullptr is of type nullptr_t . One of its big advantages compared to NULL (or to 0 would say Bjarne since he does not like macros) is that between these two functions: void foo(char*); void foo(int); foo(nullptr) will call the char* overload but foo(NULL) will call the int

Why `void* = 0` and `void* = nullptr` makes the difference?

a 夏天 提交于 2019-12-04 23:52:25
I was playing with SFINAE and found behavior I cannot explain. This compiles fine : 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) {} While this ( nullptr replaced with 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) {} gives me a compile error :

The nullptr and pointer arithmetic

亡梦爱人 提交于 2019-12-04 20:17:27
问题 Considering the following code, is it safe to do pointer arithmetic on nullptr ? I assume adding any offsets to a nullptr results in another nullptr , so far MSVC produce results as I expected, however I am a bit unsure about whether using nullptr like this is safe: float * x = nullptr; float * y = x + 31; // I assume y is a nullptr after this assigment if (y != nullptr) { /* do something */ } 回答1: You didn't define what "safe" means to you, but regardless, the code you propose has undefined

Are nullptr references undefined behaviour in C++? [duplicate]

戏子无情 提交于 2019-12-04 13:13:20
This question already has answers here : Assigning a reference by dereferencing a NULL pointer (5 answers) Closed 6 years ago . The following code fools around with nullptr pointer and reference: #include <cstdio> void printRefAddr(int &ref) { printf("printAddr %p\n", &ref); } int main() { int *ip = nullptr; int &ir = *ip; // 1. get address of nullptr reference printf("ip=%p &ir=%p\n", ip, &ir); // 2. dereference a nullptr pointer and pass it as reference printRefAddr(*ip); // 3. pass nullptr reference printRefAddr(ir); return 0; } Question : In C++ standards, are commented statements 1..3

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

五迷三道 提交于 2019-12-04 07:11:22
问题 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 ) 回答1: 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