Skip to content

Instantly share code, notes, and snippets.

View xealits's full-sized avatar
🥘

Alex Toldaiev xealits

🥘
View GitHub Profile
@xealits
xealits / factorial.cpp
Created February 20, 2022 21:39
C/C++ doctest with separate implementation file and source with tests
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#include "doctest.h"
//int factorial(int number) { return number <= 1 ? number : factorial(number - 1) * number; }
int factorial(int number) { return number <= 1 ? 1 : factorial(number - 1) * number; }
TEST_CASE("testing the factorial function") {
CHECK(factorial(0) == 1);
CHECK(factorial(1) == 1);
CHECK(factorial(2) == 2);
@xealits
xealits / keybase.md
Created January 28, 2024 17:21
keybase verification

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@xealits
xealits / basic_macro.nim
Last active February 18, 2024 21:23
Basic macro in NIM
#[
# the example is from https://peterme.net/metaprogramming-and-read-and-maintainability-in-nim.html
# plus I used `quote` instead of direct tree nodes
# it almost works the same way
# except, it seems you cannot declare an empty `enum` under `quote`
]#
import math, strutils
import macros
@xealits
xealits / pattern_command_1.cpp
Last active October 12, 2024 01:14
Simple pattern examples in C++
/*
* Just some notes while reading Game Programming Patterns by Robert Nystrom
* https://gameprogrammingpatterns.com
*
* A command or event made with C++ std::variant.
* The idea is to pack some similar-size but different types into the command.
* How to subscribe components to events from each other
* and construct it declaratively, at compile time?
* The components send events or commands (command pattern).
* You want to verify the interfaces, preferably statically, i.e. at link time.
@xealits
xealits / query_examples.md
Created January 11, 2025 15:01
clang-query matches

Find references to non-const pointees:

clang-query -c "set output dump" -c "m varDecl(hasType(referenceType(pointee(unless(isConstQualified())))))" source.cpp
@xealits
xealits / duck_typing_static.cpp
Last active May 28, 2025 21:52
Static duck typing in C++
#include <iostream>
#include <source_location>
#include <string>
struct Quack;
template <typename T>
concept Duck = requires(T t) {
{ t.quack() } -> std::convertible_to<Quack>;
// could be -> std::same_as<Quack>;
};