Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9227613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:07:22+00:00 2026-06-18T05:07:22+00:00

The canonical way to read lines from a text file is: std::fstream fs(/tmp/myfile.txt); std::string

  • 0

The canonical way to read lines from a text file is:

std::fstream fs("/tmp/myfile.txt");
std::string line;
while (std::getline(line, fs)) {
   doThingsWith(line);
}

(no, it is not while (!fs.eof()) { getline(line, fs); doThingsWith(line); }!)

This works beacuse std::getline returns the stream argument by reference, and because:

  • in C++03, streams convert to void*, via an operator void*() const in std::basic_ios, evaluating to the null pointer value when the fail error flag is set;
    • see [C++03: 27.4.4] & [C++03: 27.4.4.3/1]
  • in C++11, streams convert to bool, via an explicit operator bool() const in std::basic_ios, evaluating to false when the fail error flag is set
    • see [C++11: 27.5.5.1] & [C++11: 27.5.5.4/1]

In C++03 this mechanism means the following is possible:

std::cout << std::cout;

It correctly results in some arbitrary pointer value being output to the standard out stream.

However, despite operator void*() const having been removed in C++11, this also compiles and runs for me in GCC 4.7.0 in C++11 mode.

How is this still possible in C++11? Is there some other mechanism at work that I’m unaware of? Or is it simply an implementation “oddity”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:07:24+00:00Added an answer on June 18, 2026 at 5:07 am

    I’m reasonably certain this is not allowed/can’t happen in a conforming implementation of C++11.

    The problem, of course, is that right now, most implementations are working on conforming, but aren’t there completely yet. At a guess, for many vendors, this particular update is a fairly low priority. It improves error checking, but does little (or nothing) to enable new techniques, add new features, improve run-time efficiency, etc. This lets the compiler catch the error you’ve cited (some_stream << some_other_stream) but doesn’t really make a whole lot of difference otherwise.

    If I were in charge of updating a standard library for C++11, I think this would be a fairly low priority. There are other changes that are probably as easy (if not easier) to incorporate, and likely to make a much bigger difference to most programmers.

    To use one of the examples you gave, if I were in charge of updating the VC++ standard library to take advantage of the compiler features added in the November CTP, my top priority would probably be to add constructors to the standard container types to accept initialization_lists. These are fairly easy to add (I’d guess one person could probably add and test them in under a week) and make quite an obvious, visible difference in what a programmer can do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's a simple and canonical way to read an entire file into memory in
What's the simplest/canonical way to create an empty file in C#/.NET? The simplest way
What is the canonical way to get an empty array in Scala? new Array[String](0)
Assume SQL Server 2005+. Part A: What is the canonical way to query from
Is there a canonical way to check the size of a file in Windows?
What is the canonical way to access the MessageContext from a PayloadEndpoint? We are
Is there a canonical way to randomize an array in Objective-C?
Duplicate: What’s the canonical way to check for type in python? How do I
Is there a canonical way to test to see if the process has administrative
What is the canonical way to pass a list to a Tcl procedure? I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.