Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.


Artifact ID: ecad9f29885e9c8ee8cc0506fccb05e0ce425246e2f201f40bf2a3485bad4af3
Page Name:Supplements
Date: 2019-01-01 13:35:06
Original User: luismachuca
Parent: 070fe6c95bff0aa7dc4ce8c7751c353d87772dc0d9b3321aa38bf86958f3cc52

Cxxomfort Library Supplements

The cxxomfort library offers some specific utilities of its own, not found in C++03 or C++11.

The header cxxomfort/library.hpp> is responsible for adding these features, and automatically included when using #include <cxxomfort/cxxomfort.hpp> as well. All the features thus included are defined in namespace cxxomfort.

Unlike Extras, these features are considered intrinsic to the cxxomfort library: other features are allowed to depend on them, and they do not need to be explicitly included to work.

General index of library utilities:

Array References

#include <cxxomfort/library/array_ref.hpp>

The cxxomfort library implements n3334's array_ref<T> as a Supplement: a reference or view-type for a contiguous sequence of elements. Generated Documentation.

Foreach Loop

#include <cxxomfort/library/foreach.hpp>

"foreach" Loop emulation: allows writing C++11-like foreach loops in C++03 via a reworked implementation of the Artima Foreach utility, which also generates native foreach loops in C++11 onwards, thus resulting in zero overhead.

SomeSequence<string> mystrings;
// fill mystrings somehow
CXXO_FOREACH /string const& s, mystrings) {
    // do stuff with 's'


#include <cxxomfort/library/fixed_vector.hpp>

fixed_vector<T> - the oft sought for holy grail of a std::vector whose size is fixed at construction as an invariant. Generated Documentation.

fixed_vector<short> fixed(32);
// this vector has exactly 32 elements and can not be resized.

Initialization of Sequence Containers

#include <cxxomfort/library/i12n.hpp>

Initialization of Sequences: a way to use a syntax similar to C++11's { , , , } initialization of containers in various kinda of sequence-like containers, even in C++03.

CXXO_I12N_SEQ (vector<string>, mystrings, { "foo", "bar", "baz" } );
// operate on mystrings
cout<< mystrings[1]<< endl; // foo

Local Function Emulation

#include <cxxomfort/library/localfn.hpp>

Using local functions in template calls: a way to have locally defined functions or functors, which then can be used as arguments for eg.: STL algorithm calls. While one of the big limitations in C++03 is the inability to use local types as arguments to anything templated, this library adds a partial implementation that creates proxy functor objects, where the actual "calling" code resides out of scope, and that produces code near transpartent to C++11 onwards.

// here, X is a tag that identifies the functor type in case one needs to add state or other nontrivial behaviour
CXXO_LOCALFN(X,void(int,int)) (int x0, int x1) {
    std::cout<< '{'<< x0+1<< ','<< x1-1<< '}'<< std::endl;
function<void(int,int)> f_localfn = mi_fn; // <-- can only exist in the current scope

CXXO_LOCALFN(Neg_,int(int)) (int x) {
    return -x;
transform( begin(sequence), end(sequence), begin(sequence), negate );

Type Identification

#include <cxxomfort/library/type_name.hpp>
  • template <typename T>
    std::string type_name ()
  • std::string typeid_demangle (std::type_info const&) (2)


(1) a type T passed as a template argument; (2) a function argument resulting from typeid evaluation;

these utilities return information from typeid::name(), except this information is demangled if demangling is available.

Typesafe Enums

#include <cxxomfort/library/typesafe_enum.hpp>

typesafe_enum<T,S> - a typesafe declaration wrapper for enum types, that implements the basic idea behind the enum class... feature of C++11.

Supplemental Headers

Supplementals for <algorithm>

#include <cxxomfort/library/algorithm.hpp>

Names made available: copy_leftmost_n, copy_rightmost_n, find_inversion, fix_inversion, valcmp, transform_inplace, etc.

Listing of the functions and interfaces included in this supplement.

Supplementals for <functional>

#include <cxxomfort/library/functional.hpp>

Listing of the functions and interfaces included in this supplement.

Functional supplements to operator wrappers - Just as there exists std::plus to wrap operator+, cxxomfort provides wrappers for the assignment version of those operators.

  • plus_it - applies "+=" to its arguments.
  • minus_it - applies "-=" to its arguments.
  • multiplies_it - applies "*=" to its arguments.
  • divides_it - applies "/=" to its arguments.
  • modulus_it - applies "%=" to its arguments.
  • noop, a simple do-nothing function object for completeness.

Other features

  • functiontype - type traits for function signatures such as return type, arity, etc.
  • function_ref from LLVM.
  • function_caller - zero overhead functor for calling a named C function.

Supplementals for <iterator>

#include <cxxomfort/library/iterator.hpp>
namespace cxxomfort::library::iterator;

Names made available: ssize , absdistance , fake_iterator etc.

Listing of the functions and interfaces included in this supplement.

Supplementals for <numeric>

#include <cxxomfort/library/numeric.hpp>
namespace cxxomfort::library::numeric;

This header mostly provides static / compile time alternatives to <numeric>'s constexpr additions such as gcd and lcm, as well as a number of static / compile-time accumulators such as static_minmax and static_sum.

Listing of the functions and interfaces included in this supplement.

Supplementals for <random>

#include <cxxomfort/library/random.hpp>
namespace cxxomfort::library::random;
  • MMIX, numerical_recipes named typedefs for std linear engine.
  • reseed and randint from Fundamentals V2. * splitmix64 PRNG.

Supplementals for <string>

#include <cxxomfort/library/string.hpp>
namespace cxxomfort::library::string;

Listing of the functions and interfaces included in this supplement.

Features include:

  • Generic to_string from p0117.
  • Generic ltrim, rtrim functions.
  • string_cast.

Supplementals for <tuple>

#include <cxxomfort/library/tuple.hpp>

Features include:

  • is_tuple - type trait to recognize a tuple.
  • tuple_pop - creates a tuple with the last (rightmost) element removed.
  • tuple_shift - creates a tuple with the first (leftmost) element removed (C++11 onwards only).
  • tuple2function_t - creates a function signature type (R(T1,T2,...)) according to a list of tuple arguments (tuple<T1,T2,...>).
  • function2tuple_t - the inverse of the above.

Supplementals for <type_traits>

#include <cxxomfort/library/type_traits.hpp>

Type_traits to recognize types, that are either implemented here or pulled from other cxxomfort facilities into the header:

is_std_array --> std::array<T,N>  
is_std_byte --> std::byte
is_std_function --> std::function<Signature>
is_iterator --> Any type that specializes std::iterator_traits
is_pair --> std::pair<A,B>
is_tuple --> std::tuple<...>
is_sequence --> Any type that provides a begin() and end()
is_enumeration_like --> enum { ... } or any type that implements typesafe_enum<...>

Supplementals for <utility>

#include <cxxomfort/library/utility.hpp>

Listing of the functions and interfaces included in this supplement.

Other Features

The following traits are available in cxxomfort/utilities.hpp as assistance when writing generic / quasi-generic code.


Given a type T and a list of types A1, A2, …, A9, the trait is_any_of<T, A1, A2, …, A9> inherits from std::true_type if:

  • T is an exact cv-qualified match for any of the types A1, A2, …, A9.

In both C++03 and C++11, this trait can take up to 10 candidate types.


Given a type T, the trait result_of_function<T> defines the member type ::type, as a type to-be-determined below, if:

  • T is a type signature of a function type eg.: T = R(A1,A2, …). In this case, type: R.
  • T is a pointer type to the above eg.: T = R(*)(A1,A2,…). In this case, type is defined as in the above.
  • (Not implemented yet) T is std::random_device. In this case, type: unsigned int.

In C++03 mode, this trait can recognize function types up to 10 arguments.


Given a compile-time integer N, with N≥0, this trait defines the member type ::type, as a type to-be-determined-below, if:

  • There exists a native signed integral type I such that sizeof(I)==N. In this case, type: I.

If there is no integral type for the given value of N, instantiation of this template results in an error.

Utility Functions


This trait defines the static member variable const char* value as the literal string corresponding to the "null device" recognized by the OS in the current compiler.

  • Under POSIX, this is "/dev/null"
  • Under Microsoft Windows, this is "NUL".
  • Under other systems this is left a null-string.


C++ Features -- Back to the beginning