arduino / arduino/ArduinoCore-API

Add variadic print(String...) function

未关闭
#58 25 条评论 0 个 reaction 已指派 1 人 已被 @cmaglie 认领 在 GitHub 查看
enhancement
主要语言
C++
星标
306
派生
150
PR 合并指标
30 天内没有已合并 PR

描述

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:

``` cpp
template
size_t println(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 uses **the same size as if there was no template**.
It can also handle numbers (without format).
https://msdn.microsoft.com/en-us/library/dn439779.aspx

``` cpp
template
inline size_t println(const First& first, const Rest&... rest) {
size_t r = 0;
r+=println(first);
r+=println(rest...); // recursive call using pack expansion syntax
return r;
}
```

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

``` cpp
template
inline size_t println2(const First& first) {
return println(first);
}

template
inline size_t println2(const First& first, const Rest&... rest) {
size_t r = 0;
r+=print(first);
r+=println2(rest...); // recursive call using pack expansion syntax
return r;
}
```

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。