Unable to build with String() as an argument to String::format() since 0.4.7


#1

Continuing the discussion from Particle Firmware Update Thread:

I can no longer compile any code that uses String::format() as an argument:

Given this example from the docs:

Particle.publish("startup", String::format("frobnicator started at %s", Time.timeStr()));

Gives this compiler error:

error: cannot pass objects of non-trivially-copyable type 'class String' through '...'

Tested with both Build and Dev, after using CLI to OTA upgrade the device (so the cloud knows it is using 0.4.7)


#2

Better update to say that this happens everywhere I try to use a String() object as an argument to String::format(), not when String::format is used as an argument to another function.

This works:

String::format("frobnicator started at %s", "test");

This does not:

String::format("frobnicator started at %s", String("test"));

(updated thread title accordingly)


#3

An easy workaround is to add an explicit type cast (const char*) for the String object.

I’d guess that the recent update added an operator/overload/… that is now taken instead of the previously auto-choosen working one.

But I’m surprised that this worked before. Since which firmware version have you been using this syntax?


#4

I’ve been using it since String::format() was introduced in 0.4.6; also note that the example code in the docs (as presented in my first post) also did compile in that version. Typecasting to (const char*) was never needed prior to this point. Thanks for the workaround though!


#5

I see, so this is definetly something to bring to @mdma’s attention :wink:

Code in the docs has to work :sunglasses:


#6

This is strange. I thought the docs were updated to include the explicit c_str() call. Nothing has changed regarding String operators. I’ll look into it.

EDIT: I’ve fixed the example code in the docs. To the best of my knowledge, this would never have worked even in 0.4.6, since with a varadic call the compiler does not know the intended target type is. The fix is to add a cast as @ScruffR mentions, or add .c_str() to the string.


#7

This is still recommended in the docs, see https://docs.particle.io/reference/webhooks/#sending-complex-data

So how would I use the c_str() call in this situation?

Cheers


#8

The docs don’t necessarily show best practice but foremost easy to use and grasp samples.

What exactly do you want to know?


#9

Ok so the easy to use example is in fact unusable and that’s why I’m looking to point it out to get the docs fixed and to try and learn how it should be instead. But I think I found it now.


#10

This is the sample you linked and I don’t see why it would not work - can you elaborate?

float lat = 39.73915360;
float lng = -104.98470340;
String data = String::format(
  "{\"lat\":%f, \"lng\":%f}",
  lat, lng);
Particle.publish("Elevation", data, PRIVATE);

And in this situation there is no need for c_str() - hence the confusion.


#11

I have the coordinates coming in as Strings from the GPS library, so I needed to add the c_str() call