Cxxomfort Library Utilities

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 must be #include'd explicitly or via cxxomfort.hpp. All the features thus included are defined in namespace cxxomfort.

Foreach Loop

"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.

Sequence<string> mystrings;
// fill mystrings somehow
CXXO_FOREACH /string const& s, mystrings) {
    ....
}

Fixed_vector

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

Initialization of Sequence Containers

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.

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

Local Function Emulation

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;
} 
CXXO_LOCALFN_NAME(X,mi_fn);
function<void(int,int)> f_localfn = mi_fn; // <-- can only exist in the current scope

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

Type Identification

  • std::string typeid_demangle (std::type_info const&)

Given an argument resulting from typeid evaluation, returns the same information as typeid::name(), except this information is demangled if demangling is available.

  • template <type_name T>
    std::string type_name ()

Given a template argument type, this returns the same information as the above, except this takes into consideration cv-qualifiers and, in C++11, reference types, that are usually missed by typeid.

Supplemental Headerss

Supplemental Algorithms

As a supplement to the features of C++ Standards available in <algorithm>, cxxomfort provides a number of algorithms and generics of its own make available from <cxxomfort/library/algorithmfn.hpp>.

  • accumulate_separate - accumulates elements over a range, including a selected "separator" element over each iteration.
  • count_frequencies - counts elements in a range, keeping count of each separate value found in an external indexable range.
  • count_while - iterates over the elements of a range, keeping a count of iterations.
  • find_last_if - finds the last element in a range that fulfills a predicate.
  • transform_inplace - three-argument version of transform that transforms the same range being iterated.
  • transform_n - the "_n" counterpart to transform as copy_n is to copy.

Supplemental Operator Functors

As a supplement to the features of C++ Standards available in <functional>, cxxomfort provides a number of functional apparatuses of its own make available from <cxxomfort/library/functionalfn.hpp> and <cxxomfort/library/operatorit.hpp>.

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.

Supplemental Iterator Helpers

As a supplement to the features of C++ Standards available in <iterator>, cxxomfort provides a number of algorithms and generics of its own make available from <cxxomfort/library/iteratorfn.hpp>.

Supplemental String Utilities

As a supplement to the features of C++ Standards available in <string>, cxxomfort provides a number of algorithms and generics of its own make available from <cxxomfort/library/stringfn.hpp>.

Supplemental Tuple Features

As a supplement to the features of C++ Standards available in <tuple>, cxxomfort provides a number of algorithms and generics of its own make available from <cxxomfort/library/tuplefn.hpp>.

  • 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,...>).

Supplemental Type Traits

  • is_iterator - to recognize an iterator.
  • is_std_function - to recognize a std::function.
  • is_pair - to recognize a std::pair (in C++03, also a cxxomfort::pair).
  • is_tuple - to recognize a std::tuple.

Given a type T, the trait is_iterator inherits from std::true_type if:

  • there exists a specialization of std::iterator_traits for the given T.
  • Alternatively, T is a non-void pointer type.

Given a type T, the trait is_pair inherits from std::true_type if:

  • T is a std::pair.
  • (C++03 only) T is a cxxomfort::pair.

Similarly, is_tuple inherits from the true type if:

  • T is a std::tuple.
  • Alternatively, in C++03 mode, T is a std::tr1::tuple with up to 10 elements, or more if the compiler supports variadic templates in "emulation" mode.

Supplemental Utilities

Other Features

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

is_any_of

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.

is_sequence

Given a type T, the trait is_sequence inherits from std::true_type if:

  • T defines both a ::const_iterator and a ::value_type member types.
  • Alternatively, T is a native C array of known bounds (ie.: a T(&)[N] for a known N).

function_arity

Given a type T, the trait function_arity<T> inherits from std::integral_constant<size_t, N> for N to-be-determined below, if:

  • T is a type signature of a function type eg.: T = R(A1, A2, …). In this case, N is equal to the number of function arguments in the signature type.
  • T is a std::function for a given type signature of the above. In this case, N is equal to the number of function arguments in the signature type.
  • T is a std::pointer_to_unary_function. In this case, N= 1.
  • T is a std::pointer_to_binary_function. In this case, N=2.
  • (Not implemented yet) T is a generator type based off the templates linear_congruential_engine, substract_with_carry_engine or mersenne_twister_engine, or the type random_device. In this case, N=0.

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

If T is none of the above, an instantiation of function_arity results in an error.

result_of_function

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.

integral_of_size

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

null_device

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