Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add variadic print(String...) function #58

Open
Assignees
cmaglie
@NicoHood

Description

@NicoHood

Just found this:
http://stackoverflow.com/questions/10044449/a-function-with-variable-number-of-arguments-with-known-types-the-c11-way

Add this to Print.h:

template<typename ...TailType>size_tprintln(const String &head, TailType&&... tail)    {size_t r =0;        r+=println(head);        r+=println((tail)...);return r;    }

Now you can do stuff like this:
Serial.println("Hey", "this", "is", "a", "Test");

Every word is printed in a new line. You can also add this for Serial.print (without newline).
Maybe this can be optimized to only print the last input with a new line. An overload with two arguments is needed here I guess.

Not sure if its perfect c++, but this would give us the option to print infinite strings at once.
Maybe one can generalize this even more to also print numbers (without formating of course, just DEC).

It takes a lot of flash though. But maybe someone can optimize this further? (Now that we also have c++11 with the merged PR)

Any comments on that?

Edit: Maybe this is also relevant. This usesthe same size as if there was no template.
It can also handle numbers (without format).
https://msdn.microsoft.com/en-us/library/dn439779.aspx

template<typename First,typename... Rest>inlinesize_tprintln(const First& first,const Rest&... rest) {size_t r =0;        r+=println(first);        r+=println(rest...);// recursive call using pack expansion syntaxreturn r;    }

Another example (ln now only adds a new line at the end):

template<typename First>inlinesize_tprintln2(const First& first) {returnprintln(first);    }template<typename First,typename... Rest>inlinesize_tprintln2(const First& first,const Rest&... rest) {size_t r =0;        r+=print(first);        r+=println2(rest...);// recursive call using pack expansion syntaxreturn r;    }

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp