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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:10:48+00:00 2026-06-11T07:10:48+00:00

Common std::cin usage int X; cin >> X; The main disadvantage of this is

  • 0

Common std::cin usage

int X;
cin >> X;

The main disadvantage of this is that X cannot be const. It can easily introduce bugs; and I am looking for some trick to be able to create a const value, and write to it just once.

The naive solution

// Naive
int X_temp;
cin >> X_temp;
const int X = X_temp;

You could obviously improve it by changing X to const&; still, the original variable can be modified.

I’m looking for a short and clever solution of how to do this. I am sure I am not the only one who will benefit from a good answer to this question.

// EDIT: I’d like the solution to be easily extensible to the other types (let’s say, all PODs, std::string and movable-copyable classes with trivial constructor) (if it doesn’t make sense, please let me know in comments).

  • 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-06-11T07:10:49+00:00Added an answer on June 11, 2026 at 7:10 am

    I’d probably opt for returning an optional, since the streaming could fail. To test if it did (in case you want to assign another value), use get_value_or(default), as shown in the example.

    template<class T, class Stream>
    boost::optional<T> stream_get(Stream& s){
      T x;
      if(s >> x)
        return std::move(x); // automatic move doesn't happen since
                             // return type is different from T
      return boost::none;
    }
    

    Live example.

    To further ensure that the user gets no wall-of-overloads presented when T is not input-streamable, you can write a trait class that checks if stream >> T_lvalue is valid and static_assert if it’s not:

    namespace detail{
    template<class T, class Stream>
    struct is_input_streamable_test{
      template<class U>
      static auto f(U* u, Stream* s = 0) -> decltype((*s >> *u), int());
      template<class>
      static void f(...);
    
      static constexpr bool value = !std::is_void<decltype(f<T>(0))>::value;
    };
    
    template<class T, class Stream>
    struct is_input_streamable
      : std::integral_constant<bool, is_input_streamable_test<T, Stream>::value>
    {
    };
    
    template<class T, class Stream>
    bool do_stream(T& v, Stream& s){ return s >> v; }
    } // detail::
    
    template<class T, class Stream>
    boost::optional<T> stream_get(Stream& s){
      using iis = detail::is_input_streamable<T, Stream>;
      static_assert(iis::value, "T must support 'stream >> value_of_T'");
      T x;
      if(detail::do_stream(x, s))
        return std::move(x); // automatic move doesn't happen since
                             // return type is different from T
      return boost::none;
    }
    

    Live example.

    I’m using a detail::do_stream function, since otherwise s >> x would still be parsed inside get_stream and you’d still get the wall-of-overloads that we wanted to avoid when the static_assert fires. Delegating this operation to a different function makes this work.

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

Sidebar

Related Questions

I was advised a while ago that is was common place to use std::vector
this should be pretty common yet I find it fascinating that I couldn't find
Pay attention to base64_decode in http://www.adp-gmbh.ch/cpp/common/base64.html std::string base64_decode(std::string const& encoded_string) The function is suppose
Common scenario: I have a library that uses other libraries. For example, a math
Common problem I'm sure, but I can't figure it out. In my AndroidManifest.xml and
A common problem is that for validation you need to run the same code
A common issue I have is getting confused what $(this) is referring to. I
A common question that comes up from time to time in the world of
A common piece of code I use for simple string splitting looks like this:
(I asked a variation of this question on comp.std.c++ but didn't get an answer.)

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.