Home

Awesome

foonathan/tiny

Project Status Build Status

Note: This project is currently WIP, no guarantees are made until an 0.1 release.

This project is a C++11 library for putting every last bit to good use. It combines various techniques such as llvm::PointerIntPair, tombstones and (a custom implementation of) bitfields to write types that use as little bits as possible.

Important: This library is a low-level implementation library. It is meant for experienced C++ programmers who write foundational code, such as vocabulary types, containers etc. As such, the types used by this library should not appear in interfaces directly. Instead they are implementation details of other types. This is especially true for the _impl types such as tiny::optional_impl. Proper vocabulary types are meant to be built on top of them.

Features

Foundations

Tiny Types

Tiny types are types that are just a couple of bits in size. They cannot be stored directly but instead in a storage type:

The tiny types provided by this library:

Tombstones

Optional implementations like std::optional<T> need to have storage for T and a boolean indicating whether or not one is currently stored. Due to alignment and padding this can easily double the size beyond what is necessary.

A tombstone is a special "invalid" value of a type, like a nullptr reference. It can be used to indicate an empty optional without needing to store a boolean.

The tiny::tombstone_traits are a non-intrusive way of exposing tombstones without creating the ability to expose the invalid type states.

Vocabulary Implementation Helpers

A space efficient optional implementation can be built on top of the tombstone traits. However, certain design decisions of such vocabulary types are somewhat controversial. So instead of writing the full implementation, the library contains just the bare minimum. Proper vocabulary types can be built on top.

Those are:

FAQ

Q: Are those tricks standard conforming C++?

A: For the most part, yes: The implementations are carefully crafted to avoid undefined behavior. However, I'm certainly relying on some implementation-defined behavior. For example, the tiny::pointer_tiny_storage makes some assumptions about the integral representation of pointers that are not necessarily guaranteed, but all implementations I'm aware of work that way. Please let me know if yours don't!

Q: The tiny types behave weird when I use auto.

A: That's not a question, but yes. By their very nature I cannot expose references to a stored tiny type. Proxies are used instead, which interact poorly with auto. This is an implementation limit I can't really do anything about.

Q: It breaks when I do this!

A: Don't do that. And file an issue (or a PR, I have a lot of other projects...).

Q: This is awesome!

A: Thanks. I do have a Patreon page, so consider checking it out:

Patreon

Documentation

A full reference documentation is WIP, look at the comments in the header files for now.

Annotated tutorial style examples can be found in the example directory.

Compiler Support

The library requires a C++11 compiler. Compilers that are being tested on CI:

It only requires the following standard library headers:

The debug_assert library optionally requires cstdio for printing messages to stderr. Defining DEBUG_ASSERT_NO_STDIO disables that.

It does not use exceptions, RTTI or dynamic memory allocation.

Installation

The library is header-only and has only my debug_assert library as dependency.

If you use CMake, debug_assert will be cloned automatically if not installed on the system. You can use it with add_subdirectory() or install it and use find_package(foonathan_tiny), then link to foonathan::foonathan_tiny and everything will be setup automatically.

Planned Features