Does arduino Stream library support << and >> operators?

I'm not using these but code I'm getting from my brother has these streaming operators I'm trying to run on arduino. Are these operators supported such as:

Serial << integer_layout_string << std::endl;

Thanks!

Error message:

ASCIITable.cpp: In function 'void setup()':
ASCIITable:48: error: no match for 'operator<<' in 'Serial << integer_layout_string'
ASCIITable:48: error: 'endl' is not a member of 'std'

I really don't use these operators so hope someone can be a bit easy on me when explaining :cold_sweat:

Did you include Streaming.h?

That doesn't use the std namespace, so change std::endl to just endl.

Thanks Nick. Silly question: Are you referring to Alex Hart's Streaming library? I don't know if arduino comes with streaming.h

Yes I am. According to the .h file it is Mikal Hart's library.

Without that, I doubt that the << operator will work. And I doubt that even with it the >> operator will do anything. That's for inputting. That library does not implement operator>>.

It wouldn't be a bad thing for the Arduino developers to include. It has a zero footprint until you use it, and even then the overhead is pretty small (I say this because it is just a .h file).

As for an explanation, part of the file is this:

// Generic template
template<class T> 
inline Print &operator <<(Print &stream, T arg) 
{ stream.print(arg); return stream; }

So it basically converts something like this:

Serial << "nick";

to:

Serial.print ("nick");

Where the templated type "stream" becomes "Serial" and the thing to be printed (eg. "arg") becomes "nick". The type of the thing to be printed is T, which we don't care about.

Underneath in the file are some specializations to handle numbers (eg. ints, floats) and base conversion.

The template function returns stream, so we can concatenate them.

eg.

Serial << "foo" << "bar";

Thus the result of:

Serial << "foo"

returns Serial, so this then gets applied to:

Serial << "bar"
1 Like

Thanks Nick. I'll have to read a couple more times to fully understand. Template is going to be on my list of things to learn/relearn.