1

Poking through the example code of SDFS, I saw the following:

static ArduinoOutStream cout(Serial);
// F stores strings in flash to save RAM
cout << F("\ntype any character to start\n");

What exactly is << doing in this case? Moreover, F seems to never be defined prior to this. This seems like a useful trick, but I don't understand how it works.

full code available here: https://github.com/greiman/SdFs/blob/master/examples/SdInfo/SdInfo.ino

Bo Thompson
  • 261
  • 1
  • 3
  • 13

2 Answers2

2

What exactly is << doing in this case?

It's directing the data into an output device (stream).

Yes, it's confusing using the same symbols for more than one thing...

Moreover, F seems to never be defined prior to this.

F() is a macro. It's defined by the Arduino core in WString.h:

#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
Majenko
  • 105,851
  • 5
  • 82
  • 139
1

To address your confusion with the bitshift operator;

It indeed is commonly used as a the arithmetic operator for bitshift, but appears to be 'overloaded', so that when used with streams, it is an 'insertion operator'.

In particular, stream insertion and stream extraction overloads of operator<< and operator>> return T&.

https://en.cppreference.com/w/cpp/language/operator_arithmetic

Basically there would be two definitions of the "<<" operator function:

function <<(integer){//Shift bits}
function <<(stream){//Join streams}
aaa
  • 2,715
  • 2
  • 25
  • 40