C ++ 11和更高版本中是否存在FENV_ACCESS编译指示? [英] Does FENV_ACCESS pragma exist in C++11 and higher?

查看:89
本文介绍了C ++ 11和更高版本中是否存在FENV_ACCESS编译指示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读有关不支持FENV_ACCESS编译指示的clang的错误报告评论中:

不使用 #pragma STDC FENV_ACCESS ON 设置舍入模式会调用未定义的行为.参见C11 7.6.1/2.(此杂用语在C ++中不存在,因此< cfenv>不可用,但这不是我们的错...)

在C ++中确实不存在这种杂注,从而导致< cfenv> 不可用吗?我试图在C ++ 11标准中搜索它,但实际上根本没有提到它.是从C继承函数的原型吗?还是实际上不需要它们来避免UB,因为C ++标准没有在未使用编译指示时声明任何行为未定义的情况(因为根本没有提及编译指示)?

解决方案

我搜索了2015年标准草案文本,没有发现FENV_ACCESS. http://cppreference.com 也与此无关.

但是, http://cplusplus.com 确实提到了这一点(因为我认为它不在标准中必须假定这充其量只是咨询信息):

http://www.cplusplus.com/reference/cfenv/FENV_ACCESS/

cplusplus.com上的报价:(强调我的意思)

如果设置为on,该程序将通知编译器它可能会访问浮点环境以测试其状态标志(异常)或在默认情况下在一种控制模式下运行.

如果设置为off,则编译器可能会执行某些优化,这些优化可以颠覆这些测试和模式更改,因此,在上述情况下访问浮点环境会导致未定义的行为.>

此编译指示的默认状态是打开还是关闭取决于编译器设置和库实现.

鉴于缺乏明确性,我想尽可能避免使用它.

和以往一样,如果不可避免,我希望将其封装到一个可以针对每种体系结构进行专门测试的类中.

然后记录该类的存在以及如果升级编译器,环境或库实现时可能引起的麻烦.

更新:

在c ++标准中非常简短地提到了标头:

§26.3浮点环境[cfenv]

...

2标头定义了与C标准的7.6条相同的所有函数,类型和宏.

更新:

此处的其他信息: http://en.cppreference.com/w/cpp/preprocessor/impl

我的理解是,编译指示是由C11标准而不是C ++ 11标准定义的.因此,在c ++程序中使用严格是实现/未定义.

Reading a bug report for clang not supporting FENV_ACCESS pragma I've come across a comment:

Setting the rounding mode without using #pragma STDC FENV_ACCESS ON invokes undefined behavior. See C11 7.6.1/2. (This pragma does not exist in C++, so <cfenv> is unusable, but that's not our fault...)

Does this pragma really not exist in C++, rendering <cfenv> unusable? I've tried to search for it in the C++11 standard, but it really isn't mentioned at all. Are pragmas inherited from C along with function prototypes? Or are they actually not needed to avoid UB, since the C++ standard doesn't say anything about behavior being undefined when the pragma is not used (due to not mentioning the pragma at all)?

解决方案

I searched the 2015 standard draft text and found no occurrences of FENV_ACCESS. http://cppreference.com also has nothing about it.

However, http://cplusplus.com does mention it (since it is not in the standard I think we must assume that this is advisory information at best):

http://www.cplusplus.com/reference/cfenv/FENV_ACCESS/

Quoting from cplusplus.com: (emphasis mine)

If set to on, the program informs the compiler that it might access the floating-point environment to test its status flags (exceptions) or run under control modes other than the one by default.

If set to off, the compiler may perform certain optimizations that can subvert these tests and mode changes, and thus accessing the floating-point environment in the cases described above, causes undefined behavior.

Whether the state of this pragma by default is on or off depends on the compiler settings and library implementation.

Given the unsettling lack of clarity, I would want to avoid its use if at all possible.

As ever, if the use was unavoidable I'd want to encapsulate it into one class that I can specialise and test for each architecture.

And then document the existence of this class and the trouble it may cause if the compiler, environment or library implementation is upgraded.

Update:

There is a very brief mention of the header in the c++ standard:

§ 26.3 The floating-point environment [cfenv]

...

2 The header defines all functions, types, and macros the same as Clause 7.6 of the C standard.

Update:

Further information here: http://en.cppreference.com/w/cpp/preprocessor/impl

My reading of this is that the pragma is defined by the C11 standard, not the C++11 standard. Therefore use in a c++ program is strictly implementation/un-defined.

这篇关于C ++ 11和更高版本中是否存在FENV_ACCESS编译指示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆