无情的海湾合作委员会 C++编译器
MS VS x86 编译器对以下定义没有问题,但 GCC (ARM) 抱怨。是 GCC 愚蠢还是 MSVS_x86 太聪明?
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;
};
MS VS x86 compiler has no problem with the following definitions, but GCC (ARM) complains. Is GCC dumb or is MSVS_x86 too clever?
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;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 C++ 标准,不能将右值绑定到非常量引用。然而,微软编译器有一个邪恶的扩展允许这样做。所以g++不接受你的程序是正确的。
According to the C++ standard, you cannot bind an rvalue to a non-const reference. The Microsoft compiler has an evil extension that allows this, however. So g++ is correct not to accept your program.
g++ 的抱怨是正确的,而微软在这一点上是错误的。这段代码的问题是 checkPointIn 函数通过引用获取它的第二个参数,这意味着它必须获取一个左值(例如,一个变量,或一个取消引用的指针)。然而,checkPointInside 中的代码传入了一个临时对象,它是一个右值。由于历史原因,微软编译器允许这样做,尽管规范明确禁止这样做。通常,如果您在 Microsoft 编译器中将警告级别调高,它确实会将这段代码标记为错误。
要解决此问题,请让 checkPointIn 按值或常量引用获取其最后一个参数。后者可能是更好的选择,因为 const 引用可以在必要时绑定到右值,并避免在其他情况下进行昂贵的复制。
g++ is right to complain and Microsoft has this wrong. The problem with this code is that the checkPointIn function takes it's second parameter by reference, meaning that it must take an lvalue (a variable, or a dereferenced pointer, for example). However, the code in checkPointInside is passing in a temporary object, which is an rvalue. For historical reasons the Microsoft compiler allows this, though it's explicitly forbidden by the spec. Usually, if you crank the warning level all the way up in the Microsoft compiler, it will indeed flag this code as erroneous.
To fix this, either have checkPointIn take its last argument by value or by const reference. The latter is probably the better choice, since const references can bind to rvalues if necessary and avoid making costly copies in other cases.
您无法创建对临时变量的引用(仅适用于 C++0x 中的常量引用或右值引用)。
当您使用
CIwVec2()
作为第二个参数调用checkPoint
时,就会发生这种情况。You can't create references to temporaries (only constant references or r-value references in C++0x).
This is happening when you call
checkPoint
withCIwVec2()
as second parameter.