CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2002
    Posts
    749

    Implicit conversion loses integer precision

    Hi, ALL,
    I am doing a cross-platform programming using C++ cross-platform library.

    At one point of time my program needs to read the value from the configuration file. The value will have value of 0, 1, 2, 3 or 4 and by default it will have the value of 1. So I made the variable as "int" and used it.

    The library has a class to do so, however the class has an override for long and not int.

    So when I compile the program on Mac with Xcode I get the warning in the subject line.

    My code looks like this:

    Code:
    m_config->ReadLong( "<path/to/value/in/config", 1 );
    Is there an easy way to fix this warning?

    What surprised me is that the warning was given in OSX/Xcode only, but I'm compiling 64-bit in there. and Windows/*nix it is in 32-bit.

    Thank you.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,820

    Re: Implicit conversion loses integer precision

    It looks like for OSX long uses more binary bits than for an int. For Windows int is 32 bit and so is long. For 64 bit you need long long. For OSX long may be 64 bit. Use long for the value read (which is also fine for Windows etc) or explicitly cast the result from ReadLong() to an int

    Code:
    static_cast<int>(m_config->ReadLong( "<path/to/value/in/config", 1 ));
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured