We could separate the template definition into a header file and use it from
cpp_tools.nim.
**cpp_tools.h** :
#include <cstdint>
template<typename T>
class CppSizer
{
public:
static uint32_t getSize()
{
return sizeof(T);
}
};
Run
**cpp_tools.nim** :
import macros, strutils
macro register*(base_type: typed): typed =
let stmts = """
{.emit: "/*TYPESECTION*/ class $1_sizer : public CppSizer<$1> {};".}
proc sizeof_$1*(): cuint {.header: "../cpp_tools.h", importcpp:
"$1_sizer::getSize(@)".}
""" % [$base_type.symbol]
stmts.parseStmt
Run
This version uses `parseStmt` to produce an AST node directly from a source
string. Note that `header: "../cpp_tools.h"` only works if the nimcache
directory is in the same directory as the source files. If a proper cpp_tools
package was created and installed, we would probably get away with `header:
"cpp_tools.h"`. Maybe setting a compiler option in a .nimble file to add an
include directory would be necessary.