0

I came across this macro:

#define STR_ERROR(ecode) case ecode: return #ecode;

What does the #ecode part do?

ecode is an int, and this function returns a const char*.

I'm sure that this has been answered already, but my search-foo has abandoned me. ecode itself is specific to this code. Searching for c++ # gives generic information about macros (as well as some numbered lists relating to C++).

seqre
  • 168
  • 10
AMADANON Inc.
  • 5,753
  • 21
  • 31

1 Answers1

2

According to cppreference:

# operator before an identifier in the replacement-list runs the identifier through parameter replacement and encloses the result in quotes, effectively creating a string literal

Example from Microsoft Docs

#include <stdio.h>
#define stringer( x ) printf_s( #x "\n" )
int main() {
   stringer( In quotes in the printf function call );
   stringer( "In quotes when printed to the screen" );
   stringer( "This: \"  prints an escaped double quote" );
}

Result:

In quotes in the printf function call
"In quotes when printed to the screen"
"This: \"  prints an escaped double quote"

Google-Fu protip: I just searched for C++ macro #, the Google recommended adding operator at the end, and those docs were on the first page.

seqre
  • 168
  • 10
  • 4
    This is not a microsoft compiler specific thing. Here's a link to a compiler independent page: https://en.cppreference.com/w/cpp/preprocessor/replace#.23_and_.23.23_operators – fabian Oct 20 '21 at 19:51
  • Thanks for that. I tried the search you suggested, and there was no suggestion to adding "operator". There was a related search to ``c macro stringify``, which I didn't recognised as having any relevance. – AMADANON Inc. Oct 20 '21 at 19:57