The cxxomfort library adds various features that provide C++03, C++11 with partial support for various features from later standards. For example, you can get some C++11 features in C++03, or C++1y features in C++11 (and sometimes even in C++03!).

There is a specific featureset that is always included by default, but you can get a specific subset of features by including specific headers.

Feature List

Here follows the list of features made available by cxxomfort/cxxomfort.hpp, most of them backlifted from C++11 to C++03:

Base Features

This is the base feature set that is always included automatically when you use this library. Most of it involves backporting to C++03 some features from C++11.

(#include <cxxomfort/base.hpp>)

Standard Features

This is the "normal" feature set, which basically consists of backporting or integrating features from selected C++ headers.

(#include <cxxomfort/cxxomfort.hpp>, which also includes the above)

Cxxomfort Library Features

The library also adds some of its own assortment of features specific to cxxomfort - as in, not part of C++ revisions or proposals.

Cxxomfort Extras

The library also adds a number of backports or independent features that are considered separate and must be included individually for use, and mostly regard advanced features or C++ proposals.

Details

This library adds the features in a special namespace, ::cxxomfort. To improve portability, then the features that are present in namespace ::std in the new standard are added there via a using directive.

The library also implements some of the new keywords added in C++11, in particular static_assert.

Details of Base Features

Here follows a listing of features.

Null pointer literal

Implemented by base.hpp.

C++11 defines a "null pointer literal" as a special keyword that can be used to explicitly indicate a null pointer and its type. This allows code relying on null pointers to implement null-specific features such as function overloads, as well as avoid ambiguities due to null-to-integral promotion. In C++11, this literal is named `nullptr` and is documented here, including usage examples.

This library implements the null pointer literal in C++03 in the way recommended by the Standard (see also Meyer's implementation).

When enabled, the macro CXXOMFORT_USING_nullptr is defined.

Known limitations:

  • Requires the inclusion of a header to be used.
  • Does not implement any operators beyond implicit conversion.

Static Assert

Implements: n1604, n1720.

Implemented by base.hpp.

C++11 adds the capability to perform assertions at compile time via a special keyword. This allows code to test for prerequisites at compile-time and deliver better error messages.

The compile-time assertion feature in C++11 is documented here, including usage samples.

The static_assert keyword is implemented here as a pseudo-keyword using a macro with the same syntax; however, being a macro, it has some limitations.

(Starting in version 0.42) The extra pseudokeyword static_assert0.

When enabled, the macro CXXOMFORT_USING_static_assert is defined.

Known limitations:

  • Can not take arguments containing comas - for those, wrapping parentheses, a typedef or an enum declaration are required (and recommended even in C++11 mode, as they make code clearer).
  • The compiler won't be able to display the actual error message string, although it will at least point to its location (which, when using an IDE, should amount to the same effect).

Metaprogramming Helpers

Implementation of metaprogramming helpers such as identity, enable_if, etc.

See Features/MetaprogrammingHelpers. (To be documented)


Iterator Access Functions

Implemented by iterator.hpp.

This library adds for C++03 the following global functions present in C++11:

  • std::begin and std::end -- generically find the begin and end iterators for a container-like expression.
  • std::next and std::prev -- generically advance an iterator in forward or backward direction.

This library adds for C++03 and C++11 the following proposed global functions present in C++14:

  • std::cbegin -- generically return const_iterator with the same semantics as begin's.
  • std::cend -- generically return const_iterator with the same semantics as end's.

Each of the [c][begin,end] functions is overloaded to take container-like objects that define iterator types and C-like arrays, as well as std::valarray, which is container-like but does not define iterator types.

Finally, starting with version 0.45, the library also adds for all C++ modes the following proposed global functions present in N4017 (non-member container accessor functions):

  • cxxomfort::size -- generically return container size.

Details of Standard Features


Features from <algorithm>

To enable these features, #include the cxxomfort/algorithms.hpp header (yes, the plural is part of the name). Or just include cxxomfort/cxxomfort.hpp and it will be added automatically.

The library includes some C++11, C++14 and C++17 algorithms backported to previous standard versions:

  • The uninitialized_copy_n function.
  • The "copy_if", "copy_n" and "partition_copy" algorithms. (External Documentation)
  • The "minmax" and "minmax_element" algorithms. (External links)
  • The "all_of", "any_of", "none_of" algorithms. (External documentation)
  • The new C++11 The "is_sorted" algorithm.
  • The new C++11 "is_permutation" algorithm.
  • The new C++14 overloads for equal algorithm, specific to different-sized sequences.
  • The new C++17 clamp algorithm. (External Documentation)

Example usage:

// external
struct is_odd_t { 
    int operator() (int) const {...} 
} is_odd:

// usage vector<int> t, u; ... copy_if (begin(t), end(t), back_inserter(u), is_odd); assert (all_of(begin(u), end(u), is_odd); u.sort(); assert (is_sorted(begin(u), end(u));

  • (TOBEDONE) The "shuffle" and "random_shuffle" algorithms.

Features from <forward_list>

To enable these features, #include the cxxomfort/forward_list.hpp header. Or just include cxxomfort/cxxomfort.hpp and it will be added automatically.

As a convenience for portability, the <forward_list> header is also provided separately.

This header adds barebones, mostly usable support for the "forward_list" sequence container in C++03. A forward_list<T> behaves much like a std::list, except elements are only singly linked - thus, traversal is only possible in one direction, from begin to end.

Header Synopsis:

(pending)

The implementation is fairly incomplete as of yet, and it has some differences from the standard's provided one as I found it to be faulty.

  • push_back and emplace_back members are provided.
  • size member is provided.
  • insert member taking a iterator range is provided.
  • before_begin is not provided as of yet.
  • remove,remove_if are unimplemented as of yet.
  • erase_after is unimplemented as of yet.
  • unique is unimplemented as of yet.
  • reverse,resize are unimplemented as of yet.

The transparent header <forward_list> is also provided so that code can be written in a forwards-compatible manner using a standard #include directive. See the page for details.


Features from <functional>

To enable these features, #include the cxxomfort/functional.hpp header. Or just include cxxomfort/cxxomfort.hpp and it will be added automatically.

The library includes some C++14 backported to previous standard versions:

  • Type-transparent functors such as improved "bit_and" and "plus" (from n3421), aka: their <void> specializations.

The library also provides the following functionality for incomplete TR1 implementations:

  • The standard family of bit_and, bir_or and bit_xor functors.

Example usage:

// Generically "join" a sequence using the % operator, if so defined
class SumType;
struct Sum {
    template <typename SequenceType>
    SumType operator() (SequenceType const& ss) const {
        return accumulate (begin(vi), end(vi), modulus<void>() );
    }
};

SumType s; vector<int> vi; ...; // add content to vi somehow s= Sum()(vi); // <-- call does not need to be made aware of the type of vi's elements vector<TypeWithModulus> vt; ...; // add content to vt somehow s= Sum()(vt); // <-- call does not need to be made aware of the type of vt's elements


Features from <iterator>

To enable these features, #include the cxxomfort/iterator.hpp header. Or just include cxxomfort/cxxomfort.hpp and it will be added automatically.

The library includes some features that are available via the <iterator> header for generic management of iterators and ranges. See Iterator Helpers] for more details.


Features from <limits>

To enable these features, #include the cxxomfort/limits.hpp header, which is not aggregated automatically.

The library includes a special template integral_limits that extends std::numeric_limits for integral types by adding const min, max members rather than functions, so that the values can be accessed generically from C++03.

For a given integral type integral, the type cxxomfort::integral_limits<integral> inherits from std::numeric_limits<integral>, and adds the following member values:

template <typename integral> class integral_limits {
    // members from std::numeric_limits<integral>
    static const integral const_min = INTGRL_MIN; // implementation-defined
    static const integral const_max = INTGRL_MAX; // implementation-defined
    template <ushort> digits_base {
        uint value;
    };
};

For example, integral_limits<unsigned short>::const_min is equal to USHRT_MIN (which should be zero).

The const_min,const_max member variable has the same value as what is returned by the min(),max() member functions.

The digits_base member template, instantiated with an integer value, defines value as a member with the number of digits the type takes in the given base, similar to digits,digits10.


Features from <memory>

To enable these features, #include the cxxomfort/memory.hpp header. Or just include cxxomfort/cxxomfort.hpp and it will be added automatically.

The library includes some features that are available via the <memory> header for the management of pointers and memory:

  • The C++11 addressof function.
  • A "alignof(T)" macro that uses compiler specific to backport alignof keyword (in C++03 compilers, usually something like __alignof).
  • A "alignas" macro that uses compiler specifics to backport alignas keyword.
  • A partial implementation of C++11's aligned_storage, depending on the two above features.
  • A barebones implementation of pointer_traits and of allocator_traits to ease uniform API usage when writing eg.: containers.
  • A backport of unique_ptr for C++03, explained in its own section below.

The library implements alignof(type) by calling a compiler-specific feature, usually something like __alignof(type). For alignas(size), only implemented in GCC for the moment, __attribute__s are used.

The implementation of aligned_storage is made compiler specific and either takes advantage of a compiler-defined intrinsic (in the case of GCC, MSVC) or aligns objects to the alignment of at most the largest known alignable native member (such as long double or a member function pointer).


unique_ptr "Unique" Smart Pointer

Implemented by unique_ptr.hpp.

When using C++03 mode, this library implements the "unique_ptr" emulation created by Howard Hinnant. Once made available, the macro CXXOMFORT_USING_unique_ptr is defined.

Documentation on unique_ptr (external link). It is recommended to check this documentation as it provides both example usage and the rationale for usage cases.


Features from <cstdint>


Features from <string>

To enable these features, #include the cxxomfort/string.hpp header, which is not aggregated automatically.

The following interfaces from C++11 are defined and backported to C++03 by this header:

  • The to_string function template, which allows constructing a string from eg.: a native type, such as a long numerical expression.

The following proposed functionality for C++17 is backported to C++03→C++14 by this header:

  • The variadic version of to_[basic_string|string|wstring] alternative to to_string and to_wstring, as per p0117r0 proposal. The C++03 version supports 2 to 10 arguments.

Example code:

    time_t tt= time(0);
    struct tm tx;
    localtime_r(&tt,&tx);
    using namespace cxxomfort::string; // brings variadic to_string in
    string s= to_string(tx.tm_hour, ':', setw(2), setfill('0'), tx.tm_min);
    cout<< s<< endl;

Features from <tuple>

To enable these features, #include the cxxomfort/tuple.hpp header, which is not aggregated automatically.

The following interfaces from C++14 are defined and backported to C++03 and C++11 by this header:

  • get<Type>(tuple) - get a tuple's element by type (see: for an example).
  • apply(function,tuple) - apply a functioid to all elements of a tuple (see: for a example).

Features from <type_traits>

Various interfaces from C++'s <type_traits> header are ported here to previous versions:

  • is_lvalue_reference and is_rvalue_reference mapped from C++11 to C++03.
  • is_null_pointer from C++14 to previous Standards.
  • triviality traits (is_trivially_xxxx) for C++03.
  • make_void from C++17 to previous Standards.
  • Others.

TO BE ORGANIZED


Features from <utility>

The following interfaces are brought in from their C++ versions to previous standards:

  • The declval function to provide pseudovariables in unevaluated contexts, from C++11 to C++03.
  • The exchange function for pushing-in new values for variables, from C++14 to C++03, C++11.
  • The as_const helper for const_casts, from C++17 to all previous versions.

In C++03 mode, declval returns lvalue-references.

For C++03 mode only:

  • A pair03 class that implements std::pair with move capability.

The interface of this class is exact mirror to that of std::pair except for lacking tuple piecewise construction, but it supports movable types and is itself move-constructible and movable.

template <typename TA, TB> 
class pair03 {
    pair03 ();
    pair03 (pair03 const&);
    pair03 (rvref<pair03> &); // move-emulation constructor
    template <typename TX, typename TY>
    pair03 (pair03<TX,TY> const&);
    
    TA first;
    TB second;
     
    // various operators...
};

For C++11 mode only:

  • An implementation of integer_sequence and index_sequence.

Standard C++ Utilities

The following utilities defined in C++11 in other header files are added to C++03 with this library:

  • The "iota" algorithm. (External link)

The following utilities defined in C++14 are added to C++03/11 with this library:

  • The global cbegin and cend iterator accesors (see Iterator Helpers).