1

Is there a way to ignore characters in C++ inline?

For example in this answer I'm reading in:

istringstream foo("2000-13-30");

foo >> year;
foo.ignore();
foo >> month;
foo.ignore();
foo >> day;

But I'd like to be able to do this all inline:

foo >> year >> ignore() >> month >> ignore() >> day;

I thought this was possible in C++, but it definitely isn't compiling for me. Perhaps I'm remembering another language?

kaya3
  • 47,440
  • 4
  • 68
  • 97
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288

1 Answers1

3

foo.ignore() is a member function so it can't be used as a manipulator. It also doesn't have the correct return type and parameter declaration to be usable as one. You can easily make your own though:

std::istream& skip(std::istream& is) {
    return (is >> std::ws).ignore();
}

foo >> year >> skip >> month >> skip  >> day;
David G
  • 94,763
  • 41
  • 167
  • 253
  • +1, I was going to suggest the same. But be careful that it mixes *formatted* input (the `>> year` etc.) with *unformatted input* (`ignore()`) while using syntax normally reserved for formatted input only. – Angew is no longer proud of SO Apr 02 '15 at 13:27