getline in if语句 [英] getline in if statement

查看:134
本文介绍了getline in if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的阅读,在布尔上下文中使用的getline()将隐式转换为void*.我在网络上的任何地方都找不到对该声明的真正引用.到处都说不存在隐式转换,并且在布尔上下文中,指针应该是相同的类型(并且如果ptr == 00转换为指针ptr的类型).

From what I've read, getline() used in a Boolean context returns an implicitly conversion to void*. I haven't found anywhere on the web any real reference to this statement. Everywhere it says that implicit conversion doesn't exist and that in a Boolean context pointers should be of the same kind (and if ptr == 0 than 0 is converted to type of the pointer ptr).

在标准中也表示在布尔上下文中将其转换为未指定的布尔类型.那甚至是什么意思?

Also in the standard says in a Boolean context it is converted to an unspecified-Boolean-type. What does that even mean?

推荐答案

简而言之:

这意味着您可以在if语句中使用getline(),如果可行,则输入if语句块.

In short:

It means that you can use getline() in a if statement and if it works you enter the if statement block.

在布尔上下文中使用的

getline()返回到void*的隐式转换.

getline() used in a Boolean context returns an implicitly conversion to void*.

以上内容在技术上不正确(但这就是结果). getline()实际上返回对其使用的流的引用.在布尔上下文中使用流时,它将转换为可在布尔上下文中使用的未指定类型(C ++ 03).在C ++ 11中,已对其进行了更新,并将其转换为bool.

The above is not technically correct (but that is the result). getline() actually returns a reference to the stream it was used on. When the stream is used in a Boolean context this is converted into a unspecified type (C++03) that can be used in a Boolean context. In C++11, this was updated and it is converted to bool.

  • 如果getline()成功,它将返回处于良好状态的流.当将其转换为bool like 类型时,它将返回非空指针(C ++ 03),该指针在布尔上下文中使用时等同于true.
  • 如果getline()失败,它将返回处于错误状态的流.当将其转换为bool like 类型时,它将返回空指针(C ++ 03),该指针在布尔上下文中使用时等同于false.
  • If the getline() succeeded it returns a stream in a good state. When this is converted to a bool like type it returns a non-null pointer (C++03) which when used in a Boolean context is equivalent to true.
  • If the getline() fails it returns a stream in a bad state. When this is converted to a bool like type it returns a null pointer (C++03) which when used in a Boolean context is equivalent to false.

我在网络上的任何地方都找不到对该声明的真实引用.

I haven't found anywhere on the web any real reference to this statement.

  • 21.4.8.9插入器和提取器 [string.io]
    • 定义:std :: istream& getline(std :: istream& ;, std :: string&)
      • 21.4.8.9 Inserters and extractors [string.io]
        • Defines: std::istream& getline(std::istream&, std::string&)
          • 定义:std :: istream& getline(char_type * s,streamsize n);
          • 定义在布尔上下文中如何转换流

          在任何地方都说不存在隐式转换,并且在布尔上下文中,指针应该是相同的类型(并且如果ptr == 00转换为指针ptr的类型).

          Everywhere it says that implicit conversion doesn't exist and that in a Boolean context pointers should be of the same kind (and if ptr == 0 than 0 is converted to type of the pointer ptr).

          布尔值上下文中的空void*等效于false,其他任何void*等效于true. (尽管该类型实际上是未指定的,但是您可以将其视为void*(只是为了便于考虑).

          A null void* in a Boolean context is equivalent to false, any other void* is equivalent to true. (though the type is actually unspecified but you can think of it as a void* (just to make it easy to think about).

          在标准中也表示在布尔上下文中将其转换为未指定的布尔类型.那甚至是什么意思?

          Also in the standard says in a Boolean context it is converted to an unspecified-Boolean-type. What does that even mean?

          这意味着您可以在任何条件语句中使用它:

          It means you can use it any conditional statements:

          if (getline())
          {
               // If getline worked processes data
          }
          
          while(getline())
          {
              // getline. If it works then processes then try again.
          }
          

          这篇关于getline in if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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