Questions tagged [stream-operators]

Operators used for extracting or inserting into a stream.

In C++ operator<< is called the "Insertion Operator" and operator>> is called the "Extraction Operator"

These are commonly overloaded for individual classes for example:

struct foo{string bar;};

ostream& operator<<(ostream& os, const foo& bar){
    os << foo.bar;
    return os;
}

istream& operator>>(istream& is, const foo& bar){
    is >> foo.bar;
    return is;
}

foo can be used as follows:

foo bar;

cin >> bar;
cout << bar;
27 questions
22
votes
4 answers

calling operator<< in gdb

How do you call operator<<(std::ostream &os, const ClassX &x) from inside gdb ? In other words, how do you print an object in gdb ? call std::cout<
Ben
  • 7,372
  • 8
  • 38
  • 46
5
votes
4 answers

What's the right way to overload the stream operators << >> for my class?

I'm a bit confused about how to overload the stream operators for my class in C++, since it seems they are functions on the stream classes, not on my class. What's the normal way to do this? At the moment, for the "get from" operator, I have a…
ghallio
  • 233
  • 1
  • 3
  • 5
4
votes
1 answer

How to define output stream operator for boost log and a custom type

I was able to define the output stream operator for a simple struct, however, not for std::array. The following code fails to compile. What is wrong and how can I fix it? #include #include #include #include…
Andrew Dwojc
  • 119
  • 10
4
votes
2 answers

Trying to understand distinct() for streams in Java8

I was going through a book on Java8, where distinct was explained for stream. It is mentioned that the equality in order to produce distinct elements is determined by the implementation of hashCode() & equals() method. Therefore I wrote the below…
Chota Bheem
  • 1,106
  • 1
  • 13
  • 31
3
votes
1 answer

Overload of operator<< with basic_ostream

Why the typical header of stream manipulation with a user-defined class C is typically like this: std::ostream& operator<<(std::ostream& os, const C& c); std::istream& operator>>(std::istream& is, C&); and not like this: template
3
votes
1 answer

Why Isn't to_string Templatized?

I thought that to_string was just templatized and used stringstream under the hood. Is that not the case? I want to be able to do this: class foo{}; ostream& operator<<(ostream& os, const foo& /*bar*/){ os << "foo"; return os; } int main()…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
2
votes
1 answer

Is there an compiler bug in vc++? (I am a beginner)

There are different effect between vc++(debug mode) and g++, with this code test.hh class Test { public: int a, b, c; Test(int a, int b, int c) : a{ a }, b{ b }, c{ c } { } Test& operator++(); Test …
Steve Wilson
  • 104
  • 4
2
votes
2 answers

operator << (stream output) for nullptr

Consider a piece of generic C++ code which outputs to a stream the values of its arguments in case they are not equal: #define LOG_IF_NE(a, b) if(a != b) { \ std::cerr << "Failed because (" << ##a << "=" << (a) << \ ") != (" << ##b <<…
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
2
votes
2 answers

How to implement the extraction operator in a class?

I have a class that reads parts of a binary file into variables of different types. class Foo { public: size_t getSizeT(); float getFloat(); std::string getString(); private: std::ifstream stream; }; Now I'd like to implement the…
danijar
  • 32,406
  • 45
  • 166
  • 297
2
votes
4 answers

using nested-types of a template-class as template parameter

I want to implement a template function using nested-types of a template-class. I have just read here that it is better to implement operator << as non-member and non-friend function. Therefore I decided to move functions toStream() and…
oHo
  • 51,447
  • 27
  • 165
  • 200
2
votes
1 answer

ADL can't locate stream operator with appropriate qualifiers for a user defined type

I'm compiling an x64 service on Microsoft Windows 7 with Visual Studio 2010, using a Boost variant something like: namespace my_ns { typedef struct {} empty_t; typedef std::pair> string_t; typedef…
Matthew Reddington
  • 1,409
  • 3
  • 13
  • 24
1
vote
0 answers

Why does rvalue ostream operator<< not use perfect forwarding?

Overloads for streaming objects into std::ostream and friends tend to only be written for lvalue ostreams. Because it can be convenient to write code like std::ofstream("myFile.txt") << "foo";, the standard library is specified (in [ostream.rvalue])…
1
vote
1 answer

Making multiple operator<<() definition for a single class

So basically this is what I want to make: void someMethod() { StreamClass streamClass{}; streamClass << "Something" << "\n"; //do something streamClass.doA << "Something" << "\n"; //do another thing streamClass.doB << "Something" <<…
1
vote
1 answer

Custom stream class used as temporary

I want to code another stream class. Done it before like this: class MyStream { // ... }; template MyStream& operator <<(MyStream& s, const T& t) noexcept { std::cout << t; return s; } void f() { MyStream s; s <<…
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
1
vote
2 answers

Pass multiple arguments to stream operator

I have a class, let's call it Sample with variadic template arguments. This class contains a function run(Args... args). This class also implements a stream operator that calls this function. The class looks like this: template
Timo
  • 9,269
  • 2
  • 28
  • 58
1
2