隐式转换为basic_istream / ifstream / ofstream的布尔值在Visual Studio 2013中不起作用 [英] Implicit cast to bool of basic_istream/ifstream/ofstream doesn't work in Visual Studio 2013

查看:163
本文介绍了隐式转换为basic_istream / ifstream / ofstream的布尔值在Visual Studio 2013中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在VS 2012中编译,但在VS 2013中编译

The code below compiles in VS 2012 but not in VS 2013

std::ofstream stm;
if(stm != NULL)
{
}

在VS 2013中,您会遇到以下编译错误:

In VS 2013 you get this compilation error:


binary'!='没有找到采用左侧操作数为'std'的运算符:: ofstream'(或没有可接受的转换)

binary '!=' no operator found which takes a left-hand operand of type 'std::ofstream' (or there is no acceptable conversion)

我看了看标题,并在< xiobase> ,我发现了以下内容:

I looked at the headers and in <xiobase> and I found the following:

VS2012

ios_base::operator void *() const;

VS2013

operator void *()const 已被删除,并添加了带有显式的布尔运算符:

operator void *() const has been removed and the operator bool with explicit was added instead:

ios_base::explicit operator bool() const;

现在我的问题:


  1. 我在互联网上找不到有关此更改的任何信息。您知道是否在任何地方都有关于此更改的官方文章吗?

  2. 我有旧代码,其中频繁使用if(stm!= NULL)。出于不相关的原因,最好不要更改代码。有没有一种方法可以使其在VS 2013中进行编译而不进行更改?我找不到任何可以恢复运算符 void * 或从运算符bool()删除 explicit 的条件编译指令。

  1. I couldn't find any information about this change in the internet. Do you know if there is an official article about this change anywhere?
  2. I have legacy code where if(stm != NULL) is used a lot. For unrelated reasons it's preferable not to change the code. Is there a way to make it compile in VS 2013 without changing it? I couldn't find any conditional compilation directives that could restore operator void* or remove explicit from operator bool().

PS:gcc 4.9.0仍然具有 operator void *()const 。所以它不会有这个问题。

PS: gcc 4.9.0 still has operator void*() const. So it will not have this problem.

更新:

遗留代码编译我按照建议实现了以下重载:

To make my legacy code compile I implemented the following overloads as it was suggested:

#include <xiosbase>

bool operator==(const std::basic_ios<char, char_traits<char>> &stm, int null_val)
{
    return static_cast<bool>(stm) == null_val;
}

bool operator==(int null_val, const std::basic_ios<char, char_traits<char>> &stm)
{
    return operator==(stm, null_val);
}

bool operator!=(int null_val, const std::basic_ios<char, char_traits<char>> &stm)
{
    return !operator==(stm, null_val);
}

bool operator!=(const std::basic_ios<char, char_traits<char>> &stm, int null_val)
{
    return !operator==(stm, null_val);
}

在我的情况下, char 值类型足够,第二个参数是int,因为无论如何都不支持非NULL。

In my case the char value type was enough and the second parameter is int because something that is not NULL is not supported anyway.

推荐答案

有很多旧代码,您可能可以添加一个自定义 operator!= (和 operator == )函数,接受正确的参数:

If you have a lot of legacy code, you could probably add a custom operator!= (and operator==) function which takes the correct arguments:

bool operator!=(std::basic_ios const& ios, const void* ptr);
bool operator!=(const void* ptr, std::basic_ios const& ios);

这篇关于隐式转换为basic_istream / ifstream / ofstream的布尔值在Visual Studio 2013中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆