Static_cast and cast of value returned by a function - What is correct?

Hi folks,

I don't have a real programming issue but more a question on what the difference is; and what is probably the preferable solution.

Assume a function returning a boolean

boolean myReallyDifficultFunction(void);

I have put this into a function, because I need to call this function on different lines in my code. However, I do not need the returned value in any case. So I have basically two functions around that will be called in an upper level in my software:

void ignoreResultOfDifficultFunction_1(void) {
  (void) myReallyDifficultFunction();
}

and

boolean needResultOfDifficultFunction(void) {
  return( myReallyDifficultFunction() );
}

Here it is about the ignore-Function.

With defintion above I got the code compiled - all fine. But I found also following solution, which is said to be C++-Style:

void ignoreResultOfDifficultFunction_2(void) {
  static_cast<void>( myReallyDifficultFunction() );
}

What I don't understand is the difference to casting, e.g.,

a_long = (long) a;

And, I guess, everbody is doing this, and not writing

a_long = static_cast<long>(a);

So, my question, what is the difference? As said, the only rationale I was able to find is C++ vs C, hm, ok, I get both compiled, so I'm confused as Arduino IDE is C++, isn't it? So why do get the simple cast (not using the C++ static_cast) compiled?

Thanks in helping me to further understand.

Just lose the cast. Not using the return value is absolutely enough.

IIRC, static_cast is the modern safer version of the old c-style cast.

Well, if I create something like

void ignoreResultOfDifficultFunction_2(void) {
  boolean dummy = myReallyDifficultFunction();
}

I get following warning (adapted to this thread)

C:\Users\Sebastian_2\Documents\Projekte\Arduino\... myfile.cpp: In function 'void ignoreResultOfDifficultFunction_2()':
C:\Users\Sebastian_2\Documents\Projekte\Arduino\... myfile.cpp:60:10: warning: unused variable 'dummy' [-Wunused-variable]
  boolean dummy =ignoreResultOfDifficultFunction_2()'
          ^~~~~

So, cast to void seems required.

You don't need that function. IF you don't want to use the return function simply use your

function without using the return value. Like

... // some stuff
myReallyDifficultFunction();    // discard return value
... // some other stuff
flag = myReallyDifficultFunction();    // use return value

That's absolutely ok, und you will not get an error or warning.

You did something stupid, I know, just like the compiler.

As I said, just lose the cast.

void ignoreResultOfDifficultFunction_1(void) {
  myReallyDifficultFunction();
}

This is done all the time. How often do you see anyone use the return value from Serial.print() ?

So why return anything from it in the first place ?

Ah, ok, I guess thats it. Thanks

Well, I was trying to express with "DifficultFunction" that I don't want to create to versions of it because I spend some time that the functions does what I like to have. And for one call I need the return value, for others not.
I prefer to have just one version to maintain. And it was my concern that the defined return-value has to go "somewhere". That's why.

Unless you made your own type, boolean is not bool

Please expand on what you are saying

I STILL don't understand why you can't simply ignore the return value if you don't need it. This compiles without any errors / warnings (Preferences Compiler Warnings set to ALL):

uint8_t aFunction();

void setup() {
  uint8_t var;
  Serial.begin(115200);
  delay(1000);

  var = aFunction();
  Serial.print("aFunction Returned: ");
  Serial.println(var);

  aFunction();
  Serial.println("aFunction Return Value Ignored");
}

void loop() {
}

uint8_t aFunction() {
  uint8_t x = random(256);
  Serial.print("aFunction Picked: ");
  Serial.println(x);
  return x;
}

From Arduino.h:

typedef bool boolean;

Thanks

I think the confusion is coming from the fact that I was already applying the solution without knowing it. So I thought I have to do something differently, but I don't have (and have even already done that) ... hm, yes, kind of.
So, yes, hard to understand for others. Despite, thanks for your example, which is quite similar to what has @MicroBahner posted above (#4).