-
April 27th, 2015, 03:01 AM
#1
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?
-
April 27th, 2015, 03:25 AM
#2
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)
-
April 27th, 2015, 05:02 AM
#3
Re: Problem while porting from 2008 to 2013
Originally Posted by 2kaud
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.
-
April 27th, 2015, 05:16 AM
#4
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)
-
April 27th, 2015, 09:21 AM
#5
Re: Problem while porting from 2008 to 2013
Originally Posted by 2kaud
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|