user59065: I'm using the SPISlave library, and I have the following code snippet:

String arg;

SPISlave.onData([arg](uint8_t *data, size_t len) {
  data[len] = 0;
  arg += String((char *)data);
  // ...
}

The compiler doesn't like my string concatenation, though:

/home/lars/sketch_apr01a/sketch_apr01a.ino: In lambda function:
  sketch_apr01a:65:12: error: passing 'const String' as 'this' argument
  of 'String& String::operator+=(const String&)' discards qualifiers
  [-fpermissive]
         arg += String((char *)data);

I've tried other versions as well, with similar results:

arg.concat((char *)data)
arg = arg + String((char *)data)
arg += String((const char *)data)

So how exactly do I write this unusual operation?

user57803: #include <stdio.h>

static const int MAX_BUFFER_LENGTH = 32;
char buffer[MAX_BUFFER_LENGTH];

sprintf(buffer, "%s%s", string1, string2);


user31105: Actually for such string concatenation (which is in a function that may be called a lot), this could result in memory fragmentation, an on most Arduinos the memory will be soon too scattered that no useful memory is left.
Instead, it's better to create beforehand a buffer with the maximum size of the string you want to handle, like:
static const int MAX_BUFFER_LENGTH = 256;

char buffer[MAX_BUFFER_LENGTH];

And use the function strcat or strncat for concatenating two strings; there are generic C functions.

user7508: Your lambda is capturing arg by copy, which is most certainly not what
you want. And since it doesn't have the mutable qualifier, the
captured parameters are not modifiable. Hence the compiler error.
You could get rid of the error by qualifying the capture as mutable,
but you would then be modifying the captured copy of arg, not the
original one. The correct solution would be to capture the String by
reference instead of capturing by copy. See Lambda
expressions.
But then, I concur with Michel Keijzers that avoiding Strings altogether
is a better option.

59065: <p>I'm using the SPISlave library, and I have the following code snippet:
</p>

<pre class="lang-cpp prettyprint-override"><code>String arg;

SPISlave.onData([arg](uint8_t *data, size_t len) {
  data[len] = 0;
  arg += String((char *)data);
  // ...
}
</code></pre>

<p>The compiler doesn't like my string concatenation, though:</p>

<blockquote>
  <p>/home/lars/sketch_apr01a/sketch_apr01a.ino: In lambda function:
  sketch_apr01a:65:12: error: passing 'const String' as 'this' argument
  of 'String&amp; String::operator+=(const String&amp;)' discards qualifiers
  [-fpermissive]
         arg += String((char *)data);</p>
</blockquote>

<p>I've tried other versions as well, with similar results:</p>

<ul>
<li><code>arg.concat((char *)data)</code></li>
<li><code>arg = arg + String((char *)data)</code></li>
<li><code>arg += String((const char *)data)</code></li>
</ul>

<p>So how exactly do I write this unusual operation?</p>
 57803: <pre><code>#include &lt;stdio.h&gt;

static const int MAX_BUFFER_LENGTH = 32;
char buffer[MAX_BUFFER_LENGTH];

sprintf(buffer, "%s%s", string1, string2);
</code></pre>
 31105: <p>Actually for such string concatenation (which is in a function that may be called a lot), this could result in memory fragmentation, an on most Arduinos the memory will be soon too scattered that no useful memory is left.</p>

<p>Instead, it's better to create beforehand a buffer with the maximum size of the string you want to handle, like:</p>

<pre><code>static const int MAX_BUFFER_LENGTH = 256;

char buffer[MAX_BUFFER_LENGTH];
</code></pre>

<p>And use the function <code>strcat</code> or <code>strncat</code> for concatenating two strings; there are generic C functions.</p>
 7508: <p>Your lambda is capturing <code>arg</code> by copy, which is most certainly not what
you want. And since it doesn't have the <code>mutable</code> qualifier, the
captured parameters are not modifiable. Hence the compiler error.</p>

<p>You could get rid of the error by qualifying the capture as <code>mutable</code>,
but you would then be modifying the captured copy of <code>arg</code>, not the
original one. The correct solution would be to capture the String by
reference instead of capturing by copy. See <a href="https://en.cppreference.com/w/cpp/language/lambda" rel="nofollow noreferrer">Lambda
expressions</a>.</p>

<p>But then, I concur with Michel Keijzers that avoiding Strings altogether
is a better option.</p>
