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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:26:43+00:00 2026-05-29T06:26:43+00:00

Rationale I try to avoid assignments in C++ code completely . That is, I

  • 0

Rationale

I try to avoid assignments in C++ code completely. That is, I use only initialisations and declare local variables as const whenever possible (i.e. always except for loop variables or accumulators).

Now, I’ve found a case where this doesn’t work. I believe this is a general pattern but in particular it arises in the following situation:

Problem Description

Let’s say I have a program that loads the contents of an input file into a string. You can either call the tool by providing a filename (tool filename) or by using the standard input stream (cat filename | tool). Now, how do I initialise the string?

The following doesn’t work:

bool const use_stdin = argc == 1;
std::string const input = slurp(use_stdin ? static_cast<std::istream&>(std::cin)
                                          : std::ifstream(argv[1]));

Why doesn’t this work? Because the prototype of slurp needs to look as follows:

std::string slurp(std::istream&);

That is, the argument i non–const and as a consequence I cannot bind it to a temporary. There doesn’t seem to be a way around this using a separate variable either.

Ugly Workaround

At the moment, I use the following solution:

std::string input;
if (use_stdin)
    input = slurp(std::cin);
else {
    std::ifstream in(argv[1]);
    input = slurp(in);
}

But this is rubbing me the wrong way. First of all it’s more code (in SLOCs) but it’s also using an if instead of the (here) more logical conditional expression, and it’s using assignment after declaration which I want to avoid.

Is there a good way to avoid this indirect style of initialisation? The problem can likely be generalised to all cases where you need to mutate a temporary object. Aren’t streams in a way ill-designed to cope with such cases (a const stream makes no sense, and yet working on a temporary stream does make sense)?

  • 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-29T06:26:44+00:00Added an answer on May 29, 2026 at 6:26 am

    Why not simply overload slurp?

    std::string slurp(char const* filename) {
      std::ifstream in(filename);
      return slurp(in);
    }
    
    int main(int argc, char* argv[]) {
      bool const use_stdin = argc == 1;
      std::string const input = use_stdin ? slurp(std::cin) : slurp(argv[1]);
    }
    

    It is a general solution with the conditional operator.

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

Sidebar

Related Questions

Why is it not advisable to use JavaScript in JSP? One rationale that I
PLT Scheme 's documentation says: The rationale for providing print is that display and
I don't understand the rationale of this code, taken from javax.swing.event.EventListenerList docs: protected void
We currently use SQL Server 2005 Enterprise for our fairly large application, that has
What is the rationale behind the advocated use of the for i in xrange(...)
Is there a rationale to decide which one of try or if constructs to
I really enjoyed Jeff's post on Spartan Programming . I agree that code like
So the standard c++ library mainly contains roughly 7 categories, what's the rationale/prototype that
Rationale : In my day-to-day C++ code development, I frequently need to answer basic
What is the rationale behind making this kind of code valid in java? Does

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.