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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:31:15+00:00 2026-05-24T09:31:15+00:00

I couldn’t find the answer through searching, so next step is asking. Say I

  • 0

I couldn’t find the answer through searching, so next step is asking. Say I have some code like this:

template<class Int>
inline Int onbit(Int value, int n) {
  return value | (static_cast<Int>(1) << n);
}

If I call the code, like this onbit(A, 4), would the constant 4 be propagated through and optimized, or should I make it a template (template<int n, class Int>) so that it would be constant. Is C++0x constexpr neccessary here and if so, how exactly should I use it (should int n be const or constexpr?).

I know that constexpr would work with constant parameters, but would it partially be optimized if part of the parameters were constant and part were variable?

Summary: Is it possible for a function (it has to be inline right?) to be partially optimized with constant propagation, and, if so, what are the requirements to do it?

Even as I am writing this I am thinking that an inline function call would propagate constants…

  • 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-24T09:31:15+00:00Added an answer on May 24, 2026 at 9:31 am

    You can’t guarantee that all compilers will optimize this code, although most modern compilers (MSVC and gcc) will, at least in most cases (it depends on the context) without regard to const-ness, since the function is so simple.

    On the other hand, you can’t guarantee that any compiler will optimize it strictly better when you do use const.

    So, if optimization is important for you, in all cases the only answer is – check your assembly in the cases when it matters. Your compiler may optimize it in one file, but not the other (e.g. when this function is called deep inside a convoluted template, it is conceivable though improbable that your compiler’s optimizer will give up and not optimize it in some cases).

    I believe you should rely on const only when it’s needed for compile-time checks, and you should rely on checking generated assembly when it’s needed for run-time efficiency.

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

Sidebar

Related Questions

Couldn't find an answer to this one. I have a WPF ListView control that
I couldn't really find this in Rails documentation but it seems like 'mattr_accessor' is
I searched google but couldn't find an answer to this rather simple question. I
Couldn't find anything about this topic. I have a Windows TCP C++ server application
I couldn't find a question that would fit this purpose, so I'm asking it.
I have some data like this: 1 2 3 4 5 9 2 6
Sorry I couldn't find the best heading for this question. Following is my requirement.
Couldn't find any good information on how to do this so I thought I'd
couldn't find my solution. hope you can help. I want to have a url
Couldn't find this anywhere, maybe I'm looking for the wrong verbs. I'm trying to

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.