[英]Is this Clang's bug or my bug?
Run the the following C++ program twice. 运行以下C ++程序两次。 Once with the given destructor and once with
std::fesetround(value);
一次使用给定的析构函数,一次使用
std::fesetround(value);
removed from the destructor. 从析构函数中删除。 Why do I receive different outputs?
为什么我会收到不同的输出? Shouldn't destructor be called after function
add
? 函数
add
之后不应该调用析构函数吗? I ran both versions on http://cpp.sh/ and Clang++ 6.0, and g++ 7.2.0. 我在http://cpp.sh/和Clang ++ 6.0和g ++ 7.2.0上都运行了这两个版本。 For g++, I also included
#pragma STDC FENV_ACCESS on
in the source code, nothing changed. 对于g ++,我还在源代码中添加了
#pragma STDC FENV_ACCESS on
,没有任何更改。
#include <iostream>
#include <string>
#include <cfenv>
struct raii_feround {
raii_feround() : value(std::fegetround()) { }
~raii_feround() { std::fesetround(value); }
inline void round_up () const noexcept { std::fesetround(FE_UPWARD ); }
inline void round_down() const noexcept { std::fesetround(FE_DOWNWARD); }
template<typename T>
T add(T fst, T snd) const noexcept { return fst + snd; }
private:
int value; };
float a = 1.1;
float b = 1.2;
float c = 0;
float d = 0;
int main() {
{
raii_feround raii;
raii.round_up();
c = raii.add(a, b);
}
{
raii_feround raii;
raii.round_down();
d = raii.add(a, b);
}
std::cout << c << "\n"; // Output is: 2.3
std::cout << d << "\n"; // Output is: 2.3 or 2.29999
}
Using the floating-point environment facilities requires inserting #pragma STDC FENV_ACCESS on
into the source (or ensure that they default to on
for the implementation you are using. (Although STDC
is a C feature, the C++ standard says that these facilities are imported into C++ by the <cfenv>
header.) 使用浮点环境工具需要将
#pragma STDC FENV_ACCESS on
插入源中(或确保on
使用的实现中默认将它们默认为on
。(尽管STDC
是C功能,但C ++标准指出这些工具已导入) C ++,通过<cfenv>
标头。)
Doing so at cpp.sh results in “warning: ignoring #pragma STDC FENV_ACCESS [-Wunknown-pragmas]”. 在cpp.sh上执行此操作将导致“警告:忽略#pragma STDC FENV_ACCESS [-Wunknown-pragmas]”。
Therefore, accessing and modifying the floating-point environment is not supported by the compiler at cpp.sh. 因此,cpp.sh的编译器不支持访问和修改浮点环境。
All I needed to do was to do std::cout << std::setprecision(30);
我要做的就是做
std::cout << std::setprecision(30);
before calling std::cout
in the code ( iomanip
should be included as well). 在代码中调用
std::cout
之前(还应包括iomanip
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.