meta API documentation
Project documentation Docs

File: include/meta/meta.hpp

1// --------------------------------------------------------------------------------------------------2// SPDX-License-Identifier: Apache-2.03// SPDX-FileCopyrightText: 2026 cpdocs developers4// --------------------------------------------------------------------------------------------------5#pragma once6 7#include <concepts>8#include <cstddef>9#include <type_traits>10#include <utility>11 12/// Generic building blocks: templates, specializations, concepts and SFINAE.13namespace meta {14 15/// Types that support `+` and yield the same type.16template <typename T>17concept addable = requires(T a, T b) {18    { a + b } -> std::same_as<T>;19};20 21/// Arithmetic types, integral or floating point.22template <typename T>23concept arithmetic = std::integral<T> || std::floating_point<T>;24 25/// A fixed-size vector.26///27/// @tparam T Element type.28/// @tparam N Number of elements.29template <typename T, std::size_t N>30struct vec {31    /// Elements.32    T data[N];33 34    /// Number of elements.35    static constexpr std::size_t size() noexcept { return N; }36 37    /// Element access.38    constexpr T& operator[](std::size_t index) noexcept { return data[index]; }39};40 41/// Partial specialization for empty vectors.42template <typename T>43struct vec<T, 0> {44    /// Always zero.45    static constexpr std::size_t size() noexcept { return 0; }46};47 48/// Describes how a type is stored on disk.49template <typename T>50struct storage_traits {51    /// Bytes used on disk.52    static constexpr std::size_t bytes = sizeof(T);53};54 55/// Full specialization: booleans are stored as one byte.56template <>57struct storage_traits<bool> {58    /// Bytes used on disk.59    static constexpr std::size_t bytes = 1;60};61 62/// Sum of two values, constrained by a concept.63template <addable T>64constexpr T sum(T a, T b) {65    return a + b;66}67 68/// Scale a value; the requires clause restricts it to arithmetic types.69template <typename T>70    requires arithmetic<T>71constexpr T scale(T value, T factor) {72    return value * factor;73}74 75/// Abbreviated function template with a constrained placeholder.76constexpr auto twice(arithmetic auto value) { return value + value; }77 78/// Integer square root, enabled only for integral types (SFINAE on the return type).79template <typename T>80std::enable_if_t<std::is_integral_v<T>, T> isqrt(T value);81 82/// Floating-point square root, enabled through a defaulted template parameter (SFINAE).83template <typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>84T fsqrt(T value);85 86/// Alias template for a vector of three elements.87template <typename T>88using vec3 = vec<T, 3>;89 90/// Variable template: whether a type is stored in a single byte.91template <typename T>92inline constexpr bool is_byte_sized = storage_traits<T>::bytes == 1;93 94/// Heterogeneous tuple of values.95template <typename... Ts>96struct pack {97    /// Number of types in the pack.98    static constexpr std::size_t count = sizeof...(Ts);99};100 101/// Build a pack from values; a variadic function template.102template <typename... Ts>103constexpr pack<std::decay_t<Ts>...> make_pack(Ts&&... values) noexcept;104 105/// Base class of the curiously recurring template pattern.106template <typename Derived>107struct counter {108    /// Number of instances of `Derived` created so far.109    static std::size_t created() noexcept;110};111 112/// Applies a template template parameter to `int`.113template <template <typename> class F>114using apply_int = F<int>;115 116/// A value paired with a label; demonstrates class template argument deduction.117template <typename T>118struct labelled {119    /// The value.120    T value;121    /// Its label.122    char const* label;123};124 125/// Deduction guide: `labelled{3.0, "x"}` is a `labelled<double>`.126template <typename T>127labelled(T, char const*) -> labelled<T>;128 129} // namespace meta