cppsp v1.4.5 - @custom: Like c/c++ Macros, but Safer and Namespaced!
Source: Dev.to
Overview
@custom allows users to define their own syntax. It is a transpile‑time, pattern‑driven code generator with nested templates and namespace‑scoped features.
"..."generates code directly.- “ acts as a placeholder that is replaced by the corresponding parameter when the custom syntax is invoked.
- Generated code can appear in the global scope or inside certain cppsp keywords.
- Namespaces are limited to a single level, e.g.:
namespace n {
@custom ...
}
Example Usage
Defining a simple custom macro
@custom xxx("...", , ...)
Importing standard headers
import iostream, vector
Defining a custom class with @function
@function>
Using a custom template inside a namespace
namespace fromcpp {
@custom tem("template ")
struct T // use struct to declare a type
}
Instantiating the custom template
// C++ generics
fromcpp.tem()
function add(T a, T b) T { return a + b; }
Generating code with placeholders
@custom cs(", " >")
print(add cs(int) (1, 2))
add cs(int) (1, 2)
Generating STL containers
@custom vec("std::vector, "> ")
@custom decl(, ";")
@custom def(, "=", , ";")
vec(int); // expands to: std::vector
decl(a); // expands to: a;
vec(vec(char)); // expands to: std::vector>
def(b, {{'a','b','c','d','e'}}) // expands to: b = {'a','b','c','d','e'};
Defining a subtraction function via a custom macro
@custom subs(, " sub(", , ",", , ") { return a - b; }")
subs(int, int a, int b)
function [sub]
print("\n", sub(3, 4))
Defining a custom class with members
@custom class("class ", , "{", , "};")
class(obja,
var a int
var s, ss string
var f float
)
Defining an auto‑lambda style function
@custom auto("auto ", , " = [", , "](", , ") {", , ";};")
if (true) {
auto(x, &, int a, return a + 1)
function [x]
print("\n auto:", x(9))
}