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 374207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:23:00+00:00 2026-05-12T14:23:00+00:00

So, in a non-class type of situation, I can do something like this: int

  • 0

So, in a non-class type of situation, I can do something like this:

int val_to_check = 0;

int some_func(int param) {
  assert(val_to_check == 0);
  return param*param+param;
}

int main() {
  printf("Val: %i\n", some_func(rand()));
  return 0;
}

If val_to_check is declared const instead, the assertion can be folded away by the compiler.

I’m curious if it’s possible to get a similar constant folding with a member variable of a class. For example, can I do something like:

class Test {
public:
  Test(int val) : val_(val) {}
  int some_func(int param) {
     assert(val_ == 0);
     return param*param+param;
   }
private:
  const int val_;
};

So val_ must be known when the class is defined, a-la:

  Test my_test(0);
  printf("Val: %i\n", my_test.some_func(rand()));

(I know these are contrived examples). It seems like it should be possible to fold the assertions away sometimes, but the simple examples I’ve tested don’t seem to do it. The best I’ve gotten is moving the assertion code to the end of the function (when compiling w/ -O3)

  • 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-05-12T14:23:00+00:00Added an answer on May 12, 2026 at 2:23 pm

    In the class example you provided, there’s no way for the compiler to assume the constant is zero because you have two runtime variables:

    • Your const int val_ is only constant for each instance of the class so it can never optimise the class’s function code since it must cater for every case.
    • The example instantiation doesn’t provide a literal constant, it provides the result of rand() which is variable. It may be possible for it to optimise it out if it knows the ONLY val_ ever being provided to all instances of that class is zero.

    Have you tried providing a constant to the constructor to see if it optimises it out?

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

Sidebar

Related Questions

I have non-template class with a templatized constructor. This code compiles for me. But
I have something like the following in the header class MsgBase { public: unsigned
This works: #include<cstdio> class A{ public: A(int a):var(a){} int var; }; int f(A obj)
Situation I have a template class TIppImage<T> for image of type T . I
I have a non-Activity class (let's call it NonActivity) that needs to post a
I have a static field in a non static class. public class DBtools {
Is it OK to derive an abstract class from a non-abstract class or is
I just wrote some new utility methods within a non-page class for an existing
I'm trying to bind a function with a non-existent class. I will try to
Stack Overflow. Let's say I want to initialize a non-static class of variables, two

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.