Unforgiving GCC C ++ Compiler

The MS VS x86 compiler has no problems with the following definitions, but GCC (ARM) complains. Is GCC stupid or is MSVS_x86 too smart?

bool checkPointInside(const CIwVec2& globalPoint) { return checkPointIn(globalPoint, CIwVec2()); }; /// error: no matching function for call to 'Fair::Sprite::checkPointIn(const CIwVec2&, CIwVec2)' bool checkPointIn(const CIwVec2& globalPoint, CIwVec2& localPoint) { return false; }; 
0
source share
3 answers

According to the C ++ standard, you cannot bind the value of r to a non-constant reference. However, the Microsoft compiler has an evil extension that allows this. So g ++ doesn’t correctly accept your program.

+15
source

g ++ is the right to complain, and Microsoft has it wrong. The problem with this code is that the checkPointIn function takes a second parameter by reference, which means that it must be set to lvalue (for example, a variable or dereferenced pointer). However, the code in checkPointInside runs in a temporary object, which is an rvalue. For historical reasons, the Microsoft compiler allows this, although it is explicitly prohibited by the specification. Usually, if you run the warning level in the Microsoft compiler, it will really erroneously consider this code to be erroneous.

To fix this, either checkPointIn will take its last argument by value, or by reference to const. The latter is probably the best choice, since const links can link to rvalues, if necessary, and avoid making expensive copies in other cases.

+8
source

You cannot create temporary references (only constant references or r-value references in C ++ 0x).

This happens when you call checkPoint with CIwVec2() as the second parameter.

+4
source

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


All Articles