Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8438971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:52:26+00:00 2026-06-10T07:52:26+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:52:27+00:00Added an answer on June 10, 2026 at 7:52 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started using WordPress just a few hours ago because I need to develop
I just learned what scrapping and cUrl is few hours ago, and since then
I've just started learning Python a few hours ago, and there seems to be
I just published my application on google play a few hours ago. I'm not
I'm very new to JavaScript (just started a few hours ago and trying to
I just started using vim a few hours ago and am working on a
I just testing new features on symfony 2.1-BETA3. Few hours ago BETA4 has been
I started programming just few month ago so I am a pure beginner but
I'm working just few days with jQuery, so question might be not very interesting.
a few weeks ago a co-worker of mine spent about two hours finding out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.