CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    1,828

    Problem while porting from 2008 to 2013

    I have a source code like this.

    Code:
    std::istringstream i(svalue);
    bool converted = (i >> level != 0);
    It is working perfectly fine in VS2008. But when I ported it to 2013, I am coming across this error

    Code:
    Error 13 error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::basic_istream<char,std::char_traits<char>>' (or there is no acceptable conversion) D:\Archana\Proj - VS2013\GCF03292015\GCF\Source\MadelineDll\DrawingCanvas.cpp 1711 1 MadelineDLL
    There is a red underline inside the brackets right underneath the ! for this error. Can someone help me in rectifying this error?

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

    Re: Problem while porting from 2008 to 2013

    As bool() operator is defined for stringstream for c++11, try
    Code:
    bool converted = (i >> level);
    Last edited by 2kaud; April 27th, 2015 at 03:35 AM.
    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)

  3. #3
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Problem while porting from 2008 to 2013

    Quote Originally Posted by 2kaud View Post
    As bool() operator is defined for stringstream for c++11, try
    Code:
    bool converted = (i >> level);
    The result is still the same.

    This is the error

    Code:
    Error 13 error C2440: 'initializing' : cannot convert from 'std::basic_istream<char,std::char_traits<char>>' to 'bool' D:\Archana\Proj - VS2013\GCF03292015\GCF\Source\MadelineDll\DrawingCanvas.cpp 1711 1 MadelineDLL
    Last edited by maverick786us; April 27th, 2015 at 05:08 AM.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Problem while porting from 2008 to 2013

    The issue is the assignment. This compiles for MSVS2013
    Code:
    bool converted (i >> level );
    For an assignment, you need to cast to bool
    Code:
    bool converted = static_cast<bool>(i >> level );
    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)

  5. #5
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Problem while porting from 2008 to 2013

    Quote Originally Posted by 2kaud View Post
    The issue is the assignment
    the issue is rather that std::istream declares an explicit operator bool() ( from c++11 on ) that in turn exactly forbids such conversions via copy-initialization ( as explicit constructors do ). Why ? because in this way the standard can define contextual boolean conversions as being equivalent to "bool a(b);" hence disallowing unwanted or dangerous implicit conversions when a boolean is expected.

    so, the OP problem can be solved either via direct initialization as you suggested, or by triggering an (explicit or contextual)boolean conversion, eg

    Code:
    auto converted = bool{ i >> level };
    bool converted = !!( i >> level );
    bool converted = ( i >> level ) ? true : false;
    // ... whatever

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