Is C ++ always for comparing a member function pointer to 0?

In the following code, the statement does not occur using the Visual Studio compiler, but it occurs when compiling for iPhone using Xcode:

class X
{
public:

 virtual void A() {}
};

X x;

void main()
{
 // Define a valid member function pointer to X::A.
 void (X::*p)() = &X::A;

 assert(p != 0);
}

Is this a bug in the compiler? How else can I check for a null pointer in this case?

+3
source share
2 answers

The code is correct, the compiler does not comply with the standard that says it (section [expr.eq]using the wording from C ++ 0x draft n3225, but it should be the same in other versions):

any pointer to an element can be compared with a null pointer constant

and

, . , , .

( [conv.ptr], std::nullptr_t ++ 0x):

(5.19) prvalue , std::nullptr_t.

( [expr.const]):

, .

. ----, , , 0 . assert , , - .

Apple, -, . , , assert((intptr_t)p != 0) - .

, .

EDIT: , , ( [conv.mem]):

(4.10) ; , .

+10

iPhone .

, -, , ,

assert(&X::A == &X::A);

.

- . , .

+3

Source: https://habr.com/ru/post/1784887/


All Articles