standards-compliance

What is the “-->” operator in C++?

血红的双手。 提交于 2019-11-25 22:15:33
问题 After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated , I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4. Here\'s the code: #include <stdio.h> int main() { int x = 10; while (x --> 0) // x goes to 0 { printf(\"%d \", x); } } I\'d assume this is C, since it works in GCC as well. Where is this defined in the standard, and where has it come from? 回答1: --> is not an operator. It is in fact two separate

When does invoking a member function on a null instance result in undefined behavior?

血红的双手。 提交于 2019-11-25 21:42:04
问题 Consider the following code: #include <iostream> struct foo { // (a): void bar() { std::cout << \"gman was here\" << std::endl; } // (b): void baz() { x = 5; } int x; }; int main() { foo* f = 0; f->bar(); // (a) f->baz(); // (b) } We expect (b) to crash, because there is no corresponding member x for the null pointer. In practice, (a) doesn\'t crash because the this pointer is never used. Because (b) dereferences the this pointer ( (*this).x = 5; ), and this is null, the program enters