C++ 将零整数转换为零浮点隐式
几个月来,我有一个项目使用以下基本结构:C++ 将零整数转换为零浮点隐式,c++,ios,objective-c,llvm-clang,C++,Ios,Objective C,Llvm Clang,几个月来,我有一个项目使用以下基本结构: struct screenPoint { float x = 0, y = 0; screenPoint(float x_, float y_): x{x_}, y{y_}{} }; 一切都很好,然后升级到XCODE 8,大概编译C++有点不同。经过数小时的调试,我发现我使用的代码如下所示: screenPoint offset{0,0}; //undefined behavior? 不再保证成员为0。通常情况下,其中一个或两
struct screenPoint {
float x = 0, y = 0;
screenPoint(float x_, float y_): x{x_}, y{y_}{}
};
<>一切都很好,然后升级到XCODE 8,大概编译C++有点不同。经过数小时的调试,我发现我使用的代码如下所示:
screenPoint offset{0,0}; //undefined behavior?
不再保证成员为0。通常情况下,其中一个或两个约为2E20。我最终通过这样做解决了这个问题:
screenPoint offset{0.0f,0.0f}; //works, the members are zeroed out
<> Pc>是否有一个C++改变,不再像我所期望的那样将整数转换成浮点?< /P>
我无法在Mac或iOS模拟器的Xcode中重现这一点。然而,在4年前的iPad上运行的以下基本测试表明,{0,0}
的情况在随机情况下并不会使浮点值正好等于零:
struct screenPoint {
float x = 0, y = 0;
screenPoint(float x_, float y_): x{x_}, y{y_}{}
};
class foo{
public:
foo(int x){}
private:
screenPoint s{0,0};
};
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
while (1) {
foo f(1); //see comment below
} nib.
}
在foo f(1)
行上放置断点表明成员s
的值对于x和y并不总是0和0
<>我把C++合并到目标C文件中,以方便这个例子,但是在我的代码中,它是一个独立的C++静态库。< /p>你使用默认的苹果LLVM编译器吗?@ NaNaLover YESI很难相信编译器会把任何0个以外的东西都粘到<<代码>浮点< /代码>中。这肯定是一个实现错误。但如果能看到一个新的解决方案,那就太好了。@juanchopanza我已经添加了在iPad上重现这个问题的代码。我无法在其他架构上复制。