-2

I'm trying to get Solipsis to compile in Visual Studio 2017(it was written for VS 2005)

I can't figure out what this code is trying to do:

template<typename T> 
bool from_string( const char* Str, T & Dest )
{
    // créer un flux à partir de la chaîne donnée
    std::istringstream iss( Str );
    // tenter la conversion vers Dest
    return iss >> Dest != 0;
}

It gets the following error

1>c:\users\root\source\repos\solipsis3d\sources\modelers\mdlrtools\include\SolipsisErrorHandler.h(91): 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)
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\exception(347): note: could be 'bool std::operator !=(const std::exception_ptr &,const std::exception_ptr &) throw()' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\exception(352): note: or       'bool std::operator !=(std::nullptr_t,const std::exception_ptr &) throw()' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\exception(357): note: or       'bool std::operator !=(const std::exception_ptr &,std::nullptr_t) throw()' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\system_error(379): note: or       'bool std::operator !=(const std::error_code &,const std::error_code &) noexcept' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\system_error(384): note: or       'bool std::operator !=(const std::error_code &,const std::error_condition &) noexcept' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\system_error(389): note: or       'bool std::operator !=(const std::error_condition &,const std::error_code &) noexcept' [found using argument-dependent lookup]
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include\system_error(394): note: or       'bool std::operator !=(const std::error_condition &,const std::error_condition &) noexcept' [found using argument-dependent lookup]
1>c:\users\root\source\repos\solipsis3d\sources\modelers\mdlrtools\include\SolipsisErrorHandler.h(91): note: or       'built-in C++ operator!=(bool, int)'
1>c:\users\root\source\repos\solipsis3d\sources\modelers\mdlrtools\include\SolipsisErrorHandler.h(91): note: while trying to match the argument list '(std::basic_istream<char,std::char_traits<char>>, int)'
1>src\Object3D.cpp(220): note: see reference to function template instantiation 'bool Solipsis::from_string<bool>(const char *,T &)' being compiled
1>        with
1>        [
1>            T=bool
1>        ]
1>c:\users\root\source\repos\solipsis3d\sources\modelers\mdlrtools\include\SolipsisErrorHandler.h(91): error C2446: '!=': no conversion from 'int' to 'std::basic_istream<char,std::char_traits<char>>'
1>c:\users\root\source\repos\solipsis3d\sources\modelers\mdlrtools\include\SolipsisErrorHandler.h(91): note: Constructor for class 'std::basic_istream<char,std::char_traits<char>>' is declared 'explicit'
1>SolipsisErrorHandler.cpp

In human language what is the return value of the '>>' operator(when used as for extraction not bit shift)? What has changed about it since VS 2005 that makes the code snippet not work?

  • 2
    [std::basic_istream::operator>>](http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt) – François Andrieux Oct 24 '17 at 18:00
  • Are you asking what operator>> returns or are you asking what the function does? – François Andrieux Oct 24 '17 at 18:02
  • See [How to ask a good question](https://stackoverflow.com/help/how-to-ask). It may be a good idea to rename the question. – viddik13 Oct 24 '17 at 18:22
  • 2
    Innapropriate that this got closed, it shouldn't have been. It's a clear reproduceable code snippet that doesn't compile with any post C++11 compiler with error message he got. Rough introduction to SO for him. It should rather have been marked as a duplicate of [this question here](https://stackoverflow.com/questions/41564040/evaluating-stream-operator-as-boolean). OP can get his code compiling again by changing as follows. `return bool(iss >> Dest);` – acraig5075 Oct 25 '17 at 08:05
  • 1
    Possible duplicate of [Evaluating stream operator >> as boolean](https://stackoverflow.com/questions/41564040/evaluating-stream-operator-as-boolean) – acraig5075 Oct 27 '17 at 10:15

1 Answers1

0

I can't figure out what this code is trying to do:

The code is trying to return whether the stream extraction was successful or not.

What does the istream extraction operator >> return?

It is not the case that the return type of the operator is breaking your compilation, but rather because of a change in behaviour since C++11.

Prior to C++11 (say with VS2005) you could check for success/failure by comparing the istream object with true/false.

return iss >> Dest != 0;

You can't do that with a C++11 compilation (say with VS2017), and the reasons are given in the excellent answer to the suggested duplicate question:

Evaluating stream operator >> as boolean

Rather modernise your function by casting to a boolean.

return bool(iss >> Dest);
acraig5075
  • 9,913
  • 3
  • 29
  • 45