English 中文(简体)
发言顺序
原标题:getline in if statement

从我所读到的“getline(>)”中,在一种诱发环境下使用的“将隐含的转换为, 避免*。 我在网上发现任何真正提到这一发言的地方。 每一处都说,默示转换确实有t,在波列兰背景中,点数应当相同(如果<代码>ptr=0,则改为点代码<>ptr。

另外,在一种诱变的情况下,该标准还被转换成一种未指明的Boolean型。 这甚至意味着什么?

问题回答

In short:

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

In Long:

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.

  • 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.

我在网上发现任何真正提到这一发言的地方。

  • 21.4.8.9 Inserters and extractors [string.io]
    • Defines: std::istream& getline(std::istream&, std::string&)
  • 27.7.2.1 Class template basic_istream [istream]
    • Defines: std::istream& getline(char_type* s, streamsize n);
  • 27.5.5.1 Overview [ios.overview]
    • Defines how a stream is converted in a boolean context

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).

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).

另外,在一种诱变的情况下,该标准还被转换成一种未指明的Boolean型。 这甚至意味着什么?

这意味着你可以使用任何有条件的声明:

if (getline())
{
     // If getline worked processes data
}

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

它没有明确地在网上说它,因为它涉及把三个不同的事实混在一起。

  1. getline() returns istream&
  2. istream is convertable to void*
  3. This conversion is used when an istream is used in boolean context (such as an if or while statement).




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签