cppsp v1.4.5 - @custom: Like c/c++ Macros, but Safer and Namespaced!

Published: (February 9, 2026 at 11:16 PM EST)
2 min read
Source: Dev.to

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))
}
0 views
Back to Blog

Related posts

Read more »

C notes

c // == Preprocessor directives for include and macro definitions ==================== // The lines below demonstrate how to include headers and define macros i...

Using C# Record types with MongoDB

markdown !MongoDB Guestshttps://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%...

Maxsus belgilar | C++ boshlang'ich

String va maxsus belgilar Escape belgi string yoki tekst ichida maxsus komandalar/belgilarni yozish uchun ishlatiladi. ' ' – qo'shtirnoq ichida qo'shtirnoqni c...