static_cast / float / bitset / const怪异 [英] static_cast / float / bitset / const weirdness

查看:194
本文介绍了static_cast / float / bitset / const怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几小时前,出现了以下问题:变量不能出现在一个常量表达式中



幸运的是,所提供的答案确实解决了他的问题,但我无法重现该解决方案。
$ b

我试图简化代码甚至更多,现在我坚持以下几点:

  #include< bitset> 

int main()
{
const size_t length_1 = static_cast< const size_t>(1.0f);
std :: bitset< length_1> bits_1;
const size_t length_2 = static_cast< const size_t>(1.0f / 1.0f);
std :: bitset< length_2> bits_2;如果使用 -pedantic
$ / code>

c>,第一个例子被编译器接受,但具有除法的数字(但显然是相同的数字)会被拒绝,并显示消息length_2不能出现在常量表达式中。

没有 -pedantic 以及 -pedantic -std = c ++ 0x 任何进一步的警告。



这是 g ++ -v 的完整输出(我为德语道歉,不管怎样,你一定能得到正确的信息):

  Es werden eingebaute Spezifikationen verwendet。 
COLLECT_GCC = g ++
COLLECT_LTO_WRAPPER = / usr / lib / gcc / i686-linux-gnu / 4.6 / lto-wrapper
Ziel:i686-linux-gnu
Konfiguriert mit:。 ./src/configure -v --with-pkgversion ='Ubuntu / Linaro 4.6.3-1ubuntu5'--with-bugurl = file:///usr/share/doc/gcc-4.6/README.Bugs --enable -languages = c,c ++,fortran,objc,obj-c ++ --prefix = / usr --program-suffix = -4.6 --enable-shared --enable-linker-build-id --with-system-zlib - -libexecdir = / usr / lib --without-included-gettext --enable-threads = posix --with-gxx-include-dir = / usr / include / c ++ / 4.6 --libdir = / usr / lib --enable -nls --with-sysroot = / --enable-clocale = gnu --enable-libstdcxx-debug --enable-libstdcxx -time = yes --enable-gnu-unique-object --enable-plugin --enable- objc-gc --enable-targets = all --disable-werror --with-arch-32 = i686 --with-tune = generic --enable-checking = release --build = i686-linux-gnu --host = i686-linux-gnu --target = i686-linux-gnu
线程模型:posix
gcc-Version 4.6.3(Ubuntu / Linaro 4.6.3-1ubuntu5)

什么这是什么原因?我猜它是 1.0f beeing被认为是一些特殊的常量,因此改变了static_cast的行为?

解决方案



C ++ 03只允许符合要求的算术常量表达式 一个整型常量表达式:浮动文字(2.13.3)只有在转换为整型或枚举类型时才会出现。



尽管将 1.0f / 1.0f 看作 1 似乎是合理的,但它超出了标准。为了避免目标机器的浮点运算听起来像是对我的一个很好的解释。 GCC 4.7要求使用libgmp,libmpfr和libmpc来完成这个工作。



C ++ 11没有这样的限制。但准确性仍然是实现定义的。
只有鼓励实现才能为编译时和运行时评估提供一致的结果。

Just a few hours ago, the following question came up: Variable cannot appear in a constant-expression

Luckily for the OP, the answer provided did solve his problem, but I cannot reproduce the solution.

I've attempted to simplify the code even more and I am now stuck with the following:

#include <bitset>

int main ()
{
   const size_t length_1 = static_cast<const size_t>(1.0f);
   std::bitset<length_1> bits_1;
   const size_t length_2 = static_cast<const size_t>(1.0f / 1.0f);
   std::bitset<length_2> bits_2;
}

If compiled with -pedantic, the first example is accepted by the compiler, but the one with a division (but obviously same number) is rejected with the message "length_2 cannot appear in a constant-expression".

Without -pedantic as well as with -pedantic -std=c++0x it's accepted without any further warning.

This is the full output of g++ -v (I apologize for german, but I'm sure you get the right information anyway):

Es werden eingebaute Spezifikationen verwendet.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.6/lto-wrapper
Ziel: i686-linux-gnu
Konfiguriert mit: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread-Modell: posix
gcc-Version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 

What's the reason for this behaviour? I guess it's 1.0f beeing recognized as some special constant and therefore changing the behaviour of static_cast?

解决方案

The answer is in §5.19.

C++03 allows only arithmetic constant expressions which meet the requirements for an integral constant expression: "Floating literals (2.13.3) can appear only if they are cast to integral or enumeration types."

So while it seems reasonable to treat 1.0f/1.0f as 1 it's nevertheless beyond the standard. To avoid "floating point arithmetic of the target machine" sounds like a good explanation to me. GCC 4.7 requires libgmp, libmpfr, and libmpc to make this work.

C++11 imposes no such restrictions. But the accuracy is still implementation defined. Implementations are only "encouraged" to provide consistent results for compile-time and run-time evaluation.

这篇关于static_cast / float / bitset / const怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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