我想使用iostream和Visual C++从/向文本文件中读取和写入NaN值.当写NaN值时,我得到1.#QNAN.但是,读回来输出1.0.
float nan = std::numeric_limits<float>::quiet_NaN ();
std::ofstream os("output.txt");
os << nan ;
os.close();
Run Code Online (Sandbox Code Playgroud)
输出是1.#QNAN.
std::ifstream is("output.txt");
is >> nan ;
is.close();
Run Code Online (Sandbox Code Playgroud)
nan等于1.0.
解
最后,正如awoodland所建议的那样,我想出了这个解决方案.我选择"nan"作为NaN的字符串表示.<<和>>运算符都被覆盖.
using namespace ::std;
class NaNStream
{
public:
NaNStream(ostream& _out, istream& _in):out(_out), in(_in){}
template<typename T>
const NaNStream& operator<<(const T& v) const {out << v;return *this;}
template<typename T>
const NaNStream& operator>>(T& v) const {in >> v;return *this;}
protected:
ostream& out;
istream& in;
};
// override << operator for …Run Code Online (Sandbox Code Playgroud) 作为一名库开发人员,我想阻止我的库用户(Windows,MSVC)链接到错误的配置(不将调试库链接到他们的发布程序,反之亦然).
是否有可能在编译期间警告用户他应该链接到库的正确配置?
编辑
调试版和发布版都应该可用,以允许Windows开发人员调试他们的应用程序.因此,我的库的调试版和发行版都应该可用.
我问这个问题是因为很多对Windows初学者开发人员的支持是由他们混合调试和发布代码以及难以调试的运行时错误引起的.