-2
inline uint32_t code(uint32_t reg, Writer &writer)
{
    uint32_t ZIEL, LHS, RHS;
    if(op == OP_CONJ) {
        LHS = lhs->code(reg, writer);
        RHS = rhs->code(LHS + 1, writer);
        reg = RHS + 1;
        writer << OP_CONJ << reg << LHS << RHS;

...

Can someone maybe help me understand what "<<" does?

Here is the Writer struct :

struct Writer {
    std::ostream &os;
    inline Writer(std::ostream &_os) : os(_os) { } inline Writer &operator<<(uint32_t val) {
        os.write((const char*)&val, sizeof(uint32_t));
        return *this;
    }
fordcars
  • 457
  • 3
  • 14
  • 1
    This might help: http://www.cplusplus.com/doc/tutorial/basic_io/ –  Feb 19 '16 at 01:31
  • This should be marked as a duplicate of [What is the << operator for in C++?](https://stackoverflow.com/questions/12772076/what-is-the-operator-for-in-c). The duplicate currently linked above has been deleted. – Ilmari Karonen Jun 16 '19 at 16:01

4 Answers4

3

It's an operator! It can be translatted as "insert formatted output".

You can read more over here: http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/

vicbab
  • 31
  • 7
3

In the code you have posted the effect of << is defined as

os.write((const char*)&val, sizeof(uint32_t))

where os is a reference to a stream, initialized in code you haven't shown.

I.e. it attempts to write the bytes the of the value, raw, to the stream.

Hopefully that stream is in binary mode, not text mode, otherwise on some systems (in particular Windows) the data can be changed on the way.

This is an unconventional use of <<. Ordinarily in C++ << is either

  • left shift (the original meaning from C), or

  • formatted output (with conversion to text), or

  • adding values to a collection.

With usage like in the code shown, with something called Writer, one would expect formatted output, not binary output.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

In c++, the << operator is used in output streams while the >> operator is used for input streams.

Example usage:

int number;
cin >> number;
cout << number;

The above code when inserted into a program which uses the std namespace takes in the value of number from the keyboard and prints that value to the screen.

star2wars3
  • 83
  • 1
  • 10
0

It is overloading the operator <<. Basically, it is a function with the name <<. When you call this function using <<, C++ will set the argument to the value next to this symbol:

writer << 8;

is as if you called the function like this:

writer.<<(8); // Which won't run, but you get the idea

Actually, you can call the operator function like this:

writer.operator<<(8);
fordcars
  • 457
  • 3
  • 14