Fix compilation with libc++

libc++ (http://libcxx.llvm.org/) is used on OS X with newer XCode.
The patch is from Benjamin Piwowarski <benjamin.piwowarski@lip6.fr>, I only
added more comments.
The changes regarding implicit conversion to bool of std::iostream work
because both the C++98 and C++11 standards guarantee that boolean evaluation
of streams returns !fail(). See e.g.
http://stackoverflow.com/questions/1334858/why-dont-iostream-objects-overload-operator-bool
for details.
This commit is contained in:
Georg Baum 2014-03-04 23:04:27 +01:00
parent ab176bfbcc
commit 07afd76b7c
5 changed files with 28 additions and 5 deletions

View File

@ -994,7 +994,8 @@ bool Buffer::importString(string const & format, docstring const & contents, Err
TempFile const tempfile("Buffer_importStringXXXXXX." + fmt->extension());
FileName const name(tempfile.name());
ofdocstream os(name.toFilesystemEncoding().c_str());
bool const success = (os << contents);
// Do not convert os implicitly to bool, since that is forbidden in C++11.
bool const success = !(os << contents).fail();
os.close();
bool converted = false;

View File

@ -1472,7 +1472,8 @@ bool extractNumber(MathData const & ar, int & i)
{
idocstringstream is(charSequence(ar.begin(), ar.end()));
is >> i;
return is;
// Do not convert is implicitly to bool, since that is forbidden in C++11.
return !is.fail();
}
@ -1480,7 +1481,8 @@ bool extractNumber(MathData const & ar, double & d)
{
idocstringstream is(charSequence(ar.begin(), ar.end()));
is >> d;
return is;
// Do not convert is implicitly to bool, since that is forbidden in C++11.
return !is.fail();
}

View File

@ -17,6 +17,10 @@
#include "support/strfwd.h"
// Forward definitions do not work with libc++
// but ios_base has already been defined in strfwd
// if compiling with it
#ifndef _LIBCPP_VERSION
namespace std {
class ios_base;
@ -25,6 +29,7 @@ template<typename CharT, typename Traits> class basic_streambuf;
typedef basic_streambuf<char, char_traits<char> > streambuf;
}
#endif
namespace lyx {

View File

@ -13,6 +13,14 @@
#ifndef STRFWD_H
#define STRFWD_H
// This includes does nothing but defining _LIBCPP_VERSION
// if libc++ is used (rather than libstdc++) - we first
// check if we have at least a c++03 standard before
// including the file
#if (__cplusplus > 19971L)
#include <ciso646>
#endif
#ifdef USE_WCHAR_T
// Prefer this if possible because GNU libstdc++ has usable
@ -28,6 +36,10 @@ namespace lyx { typedef boost::uint32_t char_type; }
#endif
// Forward definitions do not work with libc++
#ifdef _LIBCPP_VERSION
#include <string>
#else
namespace std {
@ -52,6 +64,7 @@ typedef basic_ostringstream<char, char_traits<char>, allocator<char> > ostringst
} // namepace std
#endif
namespace lyx {

View File

@ -125,8 +125,10 @@ public:
iparserdocstream(idocstream & is) : is_(is) {}
/// Like std::istream::operator bool()
operator bool() const { return s_.empty() ? is_ : true; }
/// Like std::istream::operator void*()
/// Do not convert is_ implicitly to bool, since that is forbidden in C++11.
/// FIXME: Convert to operator void*() in LyX 2.2
operator bool() const { return s_.empty() ? !is_.fail() : true; }
/// change the encoding of the input stream to \p e (iconv name)
void setEncoding(std::string const & e);