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

  • Home
  • SEARCH
  • 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 8054783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:13:36+00:00 2026-06-05T08:13:36+00:00

class A{ const size_t number; public: A(size_t number): number(number) {} void f(){ //read number,

  • 0
class A{
    const size_t number;
public:
    A(size_t number): number(number) {}
    void f(){
        //read number, possibly save in CPU register
        //call a function that the compiler can't inspect
        //    so it should assume that anything in the program state changed
        //read number again
    }

There, where it reads the number again, will the compiler (if optimization is on) still assume that it cannot have changed, because it’s const, and so read the flag from the CPU register, if it was placed in one of them before?

  • 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-05T08:13:37+00:00Added an answer on June 5, 2026 at 8:13 am

    The answer is that It depends on the implementation detail of the particular compiler.
    Primary purpose of const correctness is to prevent yourself from making honest mistakes and writing more intuitive code which is easy to maintain.
    Compiler optimization should (almost)never be a criteria for making something const.

    A good compiler might apply its optimizations and inline the const variable, others may not.
    Some factors like if the address of the const variable is taken somewhere might also effect the way in how the compiler handles it.

    Last and most important regardless of how the compiler handles it once you declare a variable const, your code should always assume that it is const, it should never be modified, modifying it with any hackery will cause an Undefined Behavior.

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

Sidebar

Related Questions

I need to define a read and a print function for a class that
$ cat Const.java public class Const { String Const(String hello) { return hello; }
I have code that looks like this: class T {}; class container { const
What is the class of an embedded asset? [Embed(source=picture.png)] public static const picture:Class; what
How is memory allocated when I use: public class MyClass { public const string
I developed a generic Unsigned class, or really a class template Unsigned<size_t N> that
Why would one use func( const Class &value ) rather than just func( Class
<?php class Lala { const a = a; const b = a . b;
Consider the following class definition: class StrToTokens { StrToTokens(const char* str, const char* delimiters
I have several members in my class which are const and can therefore only

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.