Skip to content

rsl-org/kwargs

Repository files navigation

kwargs

rsl::kwargs is an experimental single-header library that allows for keyword arguments (also known as labeled or named arguments) through use of C++26 reflection (P2996).

You can find more information about it's implementation in my blog post Fun with C++26 reflection - Keyword Arguments.

Usage

This is a single-header library, to use it simply copy kwargs.h into your project's source tree.

In order to use this library, you need a compiler with P2996 reflection support. Currently only Bloomberg's experimental clang fork is supported, it's completely untested with EDG's implementation. If you require support for that, please reach out to me (ie via issue, email or discord). PRs are also welcome.

Compile with -freflection and compile and link against libc++ instead of libstdc++ (otherwise <experimental/meta> will not be found).

You can opt into wrappers around std::print, std::println and std::format with support for named arguments by defining KWARGS_FORMATTING=1.

Example

example/simple.cpp

#include <print>

#define KWARGS_FORMATTING 1
#include <kwargs.h>

template <typename T>
int test(int x, rsl::kwargs_t<T> args){
  return x * get<"y">(args, 42);
}

int main(){
  std::println("test({}, {}) -> {}", 7, "y=42",
               test(7, make_args(y=23)));
  std::println("test({}, {}) -> {}", 7, "z=42",
               test(7, make_args(z=23)));

  // optional wrappers around `std::format`, `std::println` 
  // and `std::format`
  int y = 3;
  rsl::println("foo: {foo} bar: {bar}", 
               make_args(bar=3, 
                         foo=test(7, make_args(y))));
}

Can be compiled and executed from this project's root by invoking

mkdir -p build
clang++ -std=c++26 -freflection -stdlib=libc++ -Iinclude example/simple.cpp -o build/simple
./build/simple

More examples can be found in the example subdirectory of this repository.

License

kwargs is provided under the MIT License. Feel free to use and modify it in your projects.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published