Chris Smith Chris Smith - 1 month ago 14
C++ Question

Function not implemented macro?

I'm trying to create a macro so I can specify that a function is not implemented yet (and will crash the program when invoked). Here's what I came up with:

#define NIMPL crash(__PRETTY_FUNCTION__ " not implemented yet")


And you'd use it like so:

void myFunction() {
NIMPL;
}


However, this usage causes this error:

../feta/include/feta.h:19:41: error: expected ‘)’ before string constant
#define NIMPL crash(__PRETTY_FUNCTION__ " not implemented yet")
^


crash()
is a function that accepts one string as a parameter, prints out the message, and calls
exit(1)
.

I can confirm that
__PRETTY_FUNCTION__
alone, without the concatenation works fine. Also, concatenating two strings without
__PRETTY_FUNCTION__
works fine:
"<stuff>""<other stuff>"
. But doing both at the same time does not work.

I've tried using the stringizing operator, but it did not work (if it even exists in GCC).

Answer

Standard C and C++ provide the "magic identifier" __func__, which acts as if it were the name of a static char array containing the name of the current function. (Actually the value is implementation-defined in C++.)

gcc, as an extension, provides __PRETTY_FUNCTION__, which is identical to __func__ for C, and provides some additional information for C++.

That doesn't solve the problem you're asking about, which is that string literal concatenation applies only to string literals.

You say your crash() function takes a single string as an argument.

I suggest either modifying crash() so it takes two arguments, or writing a new wrapper function that takes two arguments. You can then use:

#define NIMPL crash(__PRETTY_FUNCTION__, " not implemented yet")

(Since you're using C++ you can overload it with the same name.) Concatenating the two strings is left as an exercise.

You could also add a second string argument to your existing crash() function and give it a default value of "" to avoid breaking existing calls.