I want to wrap a C++ #define macro in a way that my Nim code is translated to this in C++:

CPP_DEFINED_MACRO(cpp_defined_identifier) {
  cpp_defined_function(param1);
  other_cpp_defined_function(param2, param3);
}
Currently, I produce the entire block with an emit pragma. Can this be done more elegantly?

If not, could this at least be accomplished with using emit only for the first and last line of the block while using a wrapped version of the C++ defined functions in between? I tried this with two separate emit s for the first and the last line, but they end up in a completely different part of the generated C++ code than the calls to the functions. This only happens when the code is in the top level of the module, but unfortunately it has to be there.

2017-07-15 09:25:57

Untested idea:

template cppDefinedMacro(def, body: untyped) =
  {.emit: "CPP_DEFINED_MACRO(", def, ") {", body, "}".}

cppDefinedMacro "yay":
  cpp_defined_function(param1)
  other_cpp_defined_function(param2, param3)

2017-07-15 13:52:25

As I said,

Currently, I produce the entire block with an emit pragma.

I'm using something similar to your solution now (emit pragma produced by macro). Good to know that there's apparently no way around the emit here, so I can stop looking for some importc/importcpp magic to accomplish this.

2017-07-17 15:53:34