Max*_*ahm 0 c++ std ternary-operator
I'm trying to clean up some code with ternary operators and I'm running into some compiler errors i can't make sense of.
The code I had before looks like this and runs fine.
if(!inFile.good())
throw -2;
getline(inFile, inLine);
Run Code Online (Sandbox Code Playgroud)
And I'm trying to clean it up using this code instead.
(inFile.good()) ? getline(inFile, inLine) : throw -2;
Run Code Online (Sandbox Code Playgroud)
But I'm getting the following errors.
g++ -w -o test orange_test.cpp
In file included from orange_test.cpp:4:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:39:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/streambuf:558:31: error: base class 'std::__1::ios_base' has private
copy constructor
_LIBCPP_EXTERN_TEMPLATE(class basic_ios<char>)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__config:462:54: note: expanded from macro '_LIBCPP_EXTERN_TEMPLATE'
#define _LIBCPP_EXTERN_TEMPLATE(...) extern template __VA_ARGS__;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/ios:305:5: note: declared private here
ios_base(const ios_base&); // = delete;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/istream:1706:31: note: implicit default copy constructor for
'std::__1::basic_ios<char>' first required here
_LIBCPP_EXTERN_TEMPLATE(class basic_istream<char>)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__config:462:54: note: expanded from macro '_LIBCPP_EXTERN_TEMPLATE'
#define _LIBCPP_EXTERN_TEMPLATE(...) extern template __VA_ARGS__;
^
./orange_model.hpp:145:23: note: implicit default copy constructor for 'std::__1::basic_istream<char>' first required here
(inFile.good()) ? getline(inFile, inLine) : throw -2;
^
1 error generated.
make: *** [main] Error 1
Run Code Online (Sandbox Code Playgroud)
I can't make any sense of it. Is there some limitation of ternary operators or scope that I'm not aware of? Any help or insight would be greatly appreciated. Thanks in advance, Max
Edit
From looking at it the main error seems to be
error: base class 'std::__1::ios_base' has private copy constructor
Run Code Online (Sandbox Code Playgroud)
It's complaining about the std::getline(inFile, inLine) function.
Answer
This code gets it running smoothly. Thanks everyone!
inFile.good() ? (void) getline(inFile, inLine) : throw -2;
Run Code Online (Sandbox Code Playgroud)
The ternary operator has a special rule when one side is a throw. Part of that rule is that the result always will be a temporary rvalue, never a reference. So the compiler needs to make a copy of the getline() return value, and this fails because the stream is non-copyable.
You can explicitly discard the getline() return value in order to avoid the copy attempt:
inFile? (void)getline(inFile, inLine) : (throw -2);
Run Code Online (Sandbox Code Playgroud)
I don't agree with your cleanup attempt, however. The original is easier to understand and maintain.
Also, did you mean to test goodness of inFile before or also after getline()? You could do
getline(inFile, inLine) || (throw -2);
Run Code Online (Sandbox Code Playgroud)
which is idiomatic is some other languages, like perl's something or die "Error message"