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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:52:47+00:00 2026-05-17T00:52:47+00:00

Possible Duplicate: Is there some ninja trick to make a variable constant after its

  • 0

Possible Duplicate:
Is there some ninja trick to make a variable constant after its declaration?

Consider the following minimal example:

void MutateData(std::string&);

int main()
{
  std::string data = "something that makes sense to humans.";

  ::MutateData(data); // Mutates 'data' -- e.g., only changes the order of the characters.

  // At this point, 'data' should never be changed.
  // Mixing the above with const-correct code seems ugly.
}

Currently, I’m doing:

namespace
{
  std::string PrepareData(std::string data)
  {
    ::MutateData(data);

    return data;
  }
}

int main()
{
  const std::string data = ::PrepareData("something that makes sense to humans.");
}

What are some elegant solutions to simulating const beyond the point of declaration?


EDIT: I forgot to clarify that I can’t easily (not my code) change MutateData.

  • 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-17T00:52:48+00:00Added an answer on May 17, 2026 at 12:52 am

    What about:

    string MakeData(string const&)
    {
        ...
        return string(...);  // for return value optimization
    }
    

    followed by

    int main()
    {
        string const& str = MakeData("Something that makes sense to humans");
    }
    

    The difference with what you do is using a const reference, and only one function. If you cannot change MutateData, do what you suggested (with the const reference though)

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

Sidebar

Related Questions

Possible Duplicate: Is there a CSS parent selector? Is there some kind of .this
Possible Duplicate: Is there any working memory profiler for Python3 I have some script
Possible Duplicate: Is there some way to compile a .NET application to native code?
Possible Duplicate: Is there a barcode recognition framework for iOS? I've done some iPhone
Possible Duplicate: getline not asking for input? There is some unique thing happening in
Possible Duplicate: Putting a whole Facebook application in the timeline? There are some apps
Possible Duplicate: Reading/Writing MS Word files in Python I know there are some libraries
Possible Duplicate: Plural form of a word Is there some existing class or library
Possible Duplicate: Check if JavaScript is enabled with PHP Is there some way to
Possible Duplicate: Is there a proved mouseOver workaround for FirefoxDriver in Selenium2? I want

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.