KUMI v5.0.0
Gorgeous Garnet
Loading...
Searching...
No Matches
C++ Vocabulary

Empty Base Optimization

A class is empty when it is a class type other than a union, with no non-static data member other than subobjects of zero size, no virtual function, no virtual base class, and no non-empty base class ([meta.unary.prop], is_empty). An object of an empty class still occupies at least one byte, so that two distinct objects have distinct addresses, unless it is a potentially-overlapping subobject, that is, a base class subobject or a member declared [[no_unique_address]] ([intro.object]/7).

The empty base optimization gives an empty base class subobject the size zero, which the standard requires for a base of a standard-layout class without non-static data members and leaves to the implementation otherwise ([intro.object]/8), and which two subobjects of similar types at the same address forbid ([intro.object]/10).

A library that stores many tag types or stateless function objects saves, through the optimization, one byte and the padding that follows it for each such object. KUMI applies it to the empty, non-final element types of kumi::tuple, and does not use [[no_unique_address]], so that an empty class costs a byte as a member, nothing as a base class, and nothing as an element of a kumi::tuple:

#include <kumi/kumi.hpp>
#include <iostream>
struct tag {};
// the empty member occupies a byte, then the padding before i
struct with_member { tag t; int i; };
// the empty base class occupies no byte
struct with_base : tag { int i; };
int main()
{
std::cout << "int " << sizeof(int) << '\n';
std::cout << "with_member " << sizeof(with_member) << '\n';
std::cout << "with_base " << sizeof(with_base) << '\n';
// the empty element of a kumi::tuple occupies no byte
std::cout << "kumi::tuple<tag, int> " << sizeof(kumi::tuple<tag, int>)
<< '\n';
}
Fixed-size collection of heterogeneous values.
Definition tuple.hpp:35

Aggregate type

An aggregate is an array, or a class with no user-declared or inherited constructor, no private or protected direct non-static data member, no private or protected direct base class, and no virtual function or virtual base class ([dcl.init.aggr]/1). Its elements are its direct base classes, then its non-static data members, which a braced list initializes in order, with the elision of the inner braces, and which a parenthesized list initializes as well since C++20, without brace elision.

kumi::tuple and kumi::record are aggregates holding a single member that stores the elements, so that a braced list initializes them by brace elision, whereas a parenthesized list such as kumi::tuple<int, float> t(1, 2.f) does not compile:

#include <kumi/kumi.hpp>
#include <iostream>
#include <type_traits>
void show(char const* text, bool b)
{
std::cout << text << ": " << std::boolalpha << b << '\n';
}
int main()
{
show("kumi::tuple<int, float> is an aggregate",
std::is_aggregate_v<kumi::tuple<int, float>>);
// brace elision initializes the elements inside the single member
std::cout << t << '\n';
// the parenthesized u(1, 2.f) does not compile,
// a parenthesized list performing no brace elision
}

Structural type

A structural type is a scalar type, an lvalue reference type, or a literal class type whose base classes and non-static data members are all public, non-mutable, and of structural types or arrays of them ([temp.param]/12).

Since C++20, the type of a non-type template parameter may be any structural type, where C++17 restricted it to integral and enumeration types, pointers and lvalue references to objects and functions, pointers to members, std::nullptr_t, and placeholder types, so that floating-point types and structural class types became valid.

KUMI takes a label as a template argument of the structural type kumi::str, as the projection get<"x"> of a record does. A kumi::tuple whose element types are structural is a valid type of a template parameter, whose values are valid template arguments, whereas std::tuple, whose libstdc++ implementation has non-public base classes, is not:

#include <kumi/kumi.hpp>
#include <iostream>
template<kumi::tuple<int, float> V> struct constant
{
static constexpr auto value = V;
};
int main()
{
// a kumi::tuple value as a template argument
using c_t = constant<kumi::tuple<int, float>{1, 2.f}>;
std::cout << c_t::value << '\n';
}

Tuple Protocol

The tuple protocol is the informal name of the conditions under which a structured binding decomposes a class through std::tuple_size, std::tuple_element, and get, a term that the standard does not define.

By [dcl.struct.bind]/7, a structured binding decomposes a type E this way when std::tuple_size<E> names a complete class type with a member named value: the binding of index i has the type std::tuple_element<i, E>::type, and it is initialized by the member function template e.get<i>() when the lookup of get in E finds a template whose first template parameter is a non-type parameter, and by get<i>(e), found by argument-dependent lookup alone, otherwise.

Structured bindings decompose two other kinds of types without the protocol ([dcl.struct.bind]/8): the arrays, and the classes whose non-static data members are all direct members of the class or of one of its base classes, accessible where the binding is declared. C++ having no reflection before C++26, a class whose members are not accessible describes its decomposition by hand, through these specializations.

KUMI reads a product type through the same three pieces, with one difference: its algorithms call get<I>(t) unqualified, which finds a free function by argument-dependent lookup and never a member function, so that a class that provides get as a member decomposes in a structured binding and fails kumi::apply. The concept kumi::concepts::product_type tests std::tuple_size and std::tuple_element alone.

A class for which these two templates are specialized, and which provides a free get<I> returning references to its members for each value category, is accepted by the algorithms of the library as kumi::tuple is, as ns::people is below.

#include <kumi/kumi.hpp>
#include <string>
#include <iostream>
#include <utility>
namespace ns
{
struct people
{
std::string name;
int age;
};
template<std::size_t I>
decltype(auto) get(people& p) noexcept
{
if constexpr (I == 0) return (p.name);
else return (p.age);
}
template<std::size_t I>
decltype(auto) get(people const& p) noexcept
{
if constexpr (I == 0) return (p.name);
else return (p.age);
}
template<std::size_t I>
decltype(auto) get(people&& p) noexcept
{
if constexpr (I == 0) return std::move(p.name);
else return std::move(p.age);
}
}
template<>
struct std::tuple_size<ns::people>
: std::integral_constant<std::size_t, 2> {};
template<> struct std::tuple_element<0, ns::people>
{
using type = std::string;
};
template<> struct std::tuple_element<1, ns::people>
{
using type = int;
};
int main()
{
ns::people p = {"Peter", 24};
// get returns references, so that kumi::for_each writes to the members
kumi::for_each([](auto& e) { e += e; }, p);
std::cout << p.name << " " << p.age << '\n';
}
constexpr for_each_t for_each
Definition for_each.hpp:141
decltype(auto) constexpr get(array< T, N > &t) noexcept
Extracts the Ith element from a kumi::array.
Definition array.hpp:672

A class becomes a record when its elements are kumi::field types, its get takes an identifier, and kumi::is_record_type_v is specialized for it, or it declares a member alias is_record_type. The class geo::point is adapted as a record this way, its get taking the identifier of a field and returning a reference for each value category:

#include <kumi/kumi.hpp>
#include <iostream>
#include <utility>
using namespace kumi::literals;
namespace geo
{
struct point
{
double x;
double y;
};
template<kumi::concepts::identifier auto ID>
decltype(auto) get(point& p) noexcept
{
if constexpr (ID == "x"_id) return (p.x);
else return (p.y);
}
template<kumi::concepts::identifier auto ID>
decltype(auto) get(point const& p) noexcept
{
if constexpr (ID == "x"_id) return (p.x);
else return (p.y);
}
template<kumi::concepts::identifier auto ID>
decltype(auto) get(point&& p) noexcept
{
if constexpr (ID == "x"_id) return std::move(p.x);
else return std::move(p.y);
}
}
// the opt-in that makes geo::point a record
template<> inline constexpr bool kumi::is_record_type_v<geo::point> = true;
template<>
struct std::tuple_size<geo::point>
: std::integral_constant<std::size_t, 2> {};
template<> struct std::tuple_element<0, geo::point>
{
using type = kumi::field<kumi::name<"x">, double>;
};
template<> struct std::tuple_element<1, geo::point>
{
using type = kumi::field<kumi::name<"y">, double>;
};
int main()
{
geo::point p = {1.5, -2.};
// get returns references, so that kumi::for_each scales the members
kumi::for_each([](auto& e) { e *= 2; }, p);
auto print = [](auto n, auto e) { std::cout << n << " = " << e << '\n'; };
}
Concept specifying a type follows the Record Type semantic.
Definition concepts.hpp:94
constexpr for_each_field_t for_each_field
Definition for_each.hpp:241
constexpr bool is_record_type_v
Definition traits.hpp:69

Containers

A container of the C++ standard library stores objects of a single value type. The library provides sequence containers, such as std::vector and std::array, associative containers, such as std::set, and unordered associative containers, such as std::unordered_map, beside the container adaptors, such as std::stack. A sequence container of fixed size and contiguous storage resembles a homogeneous product type, and std::array models kumi::concepts::product_type.

KUMI describes containers by two concepts of its own: kumi::concepts::container accepts the built-in arrays and the classes with the member types value_type and size_type and the functions size, begin, and end, and kumi::concepts::static_container adds a size known at compile time, read from a built-in array or from a class template of the form Container<T, N>:

#include <kumi/kumi.hpp>
#include <array>
#include <iostream>
#include <vector>
void show(char const* text, bool b)
{
std::cout << text << ": " << std::boolalpha << b << '\n';
}
int main()
{
// a size known at compile time
show("std::array is a static container",
static_container<std::array<int, 3>>);
show("int[3] is a static container", static_container<int[3]>);
// a size known at run time only
show("std::vector is a container", container<std::vector<int>>);
show("std::vector is a static container",
static_container<std::vector<int>>);
}
Concept specifying a type follows the Container Type semantic.
Definition concepts.hpp:129
Concept specifying a type follows the Container Type semantic.
Definition concepts.hpp:143
Previous Next
Bibliography Nomenclature