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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:54:41+00:00 2026-06-08T11:54:41+00:00

When I compile the following code, compiler gives me the warning: Implicit conversion loses

  • 0

When I compile the following code, compiler gives me the warning:

"Implicit conversion loses integer precision: 'std::streamsize' (aka 'long') to 'int'". 

I’m a little bit confused about this warning since I just try to save the current value of the precision to set it back to the original value later.

#include <iomanip>
#include <iostream>

int main() {
  std::streamsize prec = std::cout.precision();
  std::cout << std::setprecision(prec);
}

What is the right way to save the precision value and set it back later in this case?

  • 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-08T11:54:42+00:00Added an answer on June 8, 2026 at 11:54 am

    It looks like it’s just an oversight in the standard specification.

    ios_base::precision has two overloads, one that gets and one that sets the precision:

    // returns current precision
    streamsize precision() const;
    
    // sets current precision and returns old value
    streamsize precision(streamsize prec) const;
    

    So this code will not give you warnings:

    #include <iostream>
    
    int main() {
      std::streamsize prec = std::cout.precision(); // gets
      std::cout.precision(prec); // sets
    }
    

    However, the setprecision() function simply takes a plain old int:

    unspecified-type setprecision(int n);
    

    and returns an unspecified functor, which when consumed by a stream str has the effect of:

    str.precision(n);
    

    In your case, streamsize is not an int (and does not have to be), hence the warning. The standard should probably be changed so that setprecision‘s parameter is not int, but streamsize.

    You can either just call precison() yourself, as above, or assume int is sufficient and cast.

    #include <iomanip>
    #include <iostream>
    
    int main() {
      std::streamsize prec = std::cout.precision();
      std::cout << std::setprecision(static_cast<int>(prec));
    }
    

    Edit: Apparently it was submitted to be fixed and reached no concensus (closed as not-a-defect).

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

Sidebar

Related Questions

The following code char buffer[BSIZE]; ... if(buffer[0]==0xef) ... Gives the compiler warning comparison is
The eclipse compiler refuses to compile the following code, stating that the field s
I get Compiler Error C2248 when i try to compile the following code: #include
When trying to compile the following code, I am getting a warning that line
The following code gives me the warning Contract class 'FooContracts' should be an abstract
g++ gives warning array subscript is above array bound... when I compile the following
The compiler, given the following code, tells me Use of unassigned local variable 'x'.
When I compile the following code, I only see the error during Run-time which
When I compile the following code with g++, the object of class A seems
Why won't the following C code compile? It seems like it should just change

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.