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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:07:57+00:00 2026-05-16T14:07:57+00:00

Would it make sense to have a constify operation in C/C++ that makes a

  • 0

Would it make sense to have a “constify” operation in C/C++ that makes a variable const?

Here is an example where it could be useful, where obviously we don’t want to declare it const yet in the first line:

std::vector<int> v;
v.push_back(5);
constify v; // now it's const

Currently, without such a possibility, you’d have to introduce another variable to get the same effect:

std::vector<int> v0;
v0.push_back(5);
const std::vector<int>& v = v0;

That’s more confusing since it adds a new name into the scope and you need to make it a reference to avoid copying the whole vector (or use swap?).

  • 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-16T14:07:58+00:00Added an answer on May 16, 2026 at 2:07 pm

    Frankly, I find it less confusing if a variable is either const or not, than if this can change.


    To elaborate a bit on this: The reason you usually want to do this is because you cannot initialize a const variable the way you want to. std::vector is a good example of this. Well, for once, the next standard introduces a universal initialization syntax that makes this possible:

    const std::vector<int> cvi = { 1, 2, 3, 4, 5, 42 }; 
    

    However, even without C++1x’ stuff at hand, and even with types that disallow this initialization syntax, you can always create a helper function to do what you want:

    const std::vector<int>& cvi = create_my_vector();
    

    or, if you want to be fancy:

    const std::vector<int>& cvi = compile_time_list<1,2,3,4,5,42>::create_vector();
    

    Note the &. There’s no point in copying the result of the function call, since binding an rvalue to a const reference extends its lifetime until the end of the reference’s lifetime.
    Of course, recompiling with a compiler that supports C++1x’ move semantics will render such optimizations pretty much needless. But binding an rvlaue to a const reference might still be faster than moving a vector and is unlikely to be slower.
    With C++1x, you might also create lambda functions doing this one the fly. C++ just provides an incredibly huge arsenal of tools. IME, no matter how hard you have thought, someone else ought to come up with yet another idea to do the same thing. And often a better one than yours.


    However, IME this problem usually only comes with too much code in too few functions anyway. And then it doesn’t only apply to constness, but also to similar traits – like what a reference refers to.
    A classic is the use-one-of-several-possible-streams. Instead of this

    int main(int argc, char* argv[])
    {
      std::istream* istrm = NULL;
      std::ifstream ifs;
      if( argc > 1 )
      {
        ifs.open( argv[1] );
        if( ifs.good() ) 
          istrm = &ifs;
      }
      if( !istrm ) 
        istrm = &std::cin;
    
      while( istrm->good() )
      {
         // reading from *istrm implemented here
      }
      return 0;
    }
    

    just split the concerns into 1) figuring out where to read from and 2) the actual reading:

    int read(std::istream& is)
    {
      while( is.good() )
      {
         // reading from is implemented here
      }
      return 0;
    }
    
    int main(int argc, char* argv[])
    {
      if( argc > 1 )
      {
        std::ifstream ifs( argv[1] );
        if( ifs.good() ) 
          return read(ifs);
      }
      return read(std::cin);
    }
    

    I have yet to see a real-world example of a variable that wasn’t as constant as it could have been which couldn’t be fixed by separating of concerns.

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

Sidebar

Related Questions

In designing a class for customer for example would it make sense to use
Would it not make sense to support a set of languages (Java, Python, Ruby,
I'm in the process of planning an application that would make use of the
Can anyone help me see in what kind of scenarios it would make sense
Is the type check a mere integer comparison? Or would it make sense to
I'm working at an app which would make a POST ajax request to a
I am messing around with Doctrine (version 1.0.3) to see if would make a
I'm coming from a web-development background and I am wondering how I would make
As prescribed by Yahoo!, gzip'ng files would make your websites load faster. The problem?
How would you make the contents of Flex RIA applications accessible to Google, so

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.