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

Overview

Artifact ID: 070fe6c95bff0aa7dc4ce8c7751c353d87772dc0d9b3321aa38bf86958f3cc52
Page Name:Supplements
Date: 2018-12-21 21:38:45
Original User: luismachuca
Parent: a1176ea6ed5180efa06e19b14ea92246fbca8b0574e9403066777f9047cbb101
Content

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

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.

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

Fixed_vector

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

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

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.

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

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

  • template <typename T>
    std::string type_name ()
    (1)
  • std::string typeid_demangle (std::type_info const&) (2)

Given:

(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

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>

As a supplement to the features in <algorithm>, cxxomfort provides a number of facilities of its own in <cxxomfort/library/algorithm.hpp>.

Supplementals for <functional>

As a supplement to the features in <functional>, cxxomfort provides a number of facilities of its own in <cxxomfort/library/functionalfn.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.
  • noop, a simple do-nothing function object for completeness.

Supplementals for <iterator>

As a supplement to the features in <iterator>, cxxomfort provides a number of facilities of its own in <cxxomfort/library/iterator.hpp>, including some proposed pxxxrx proposals to Standard.

Supplementals for <numeric>

As a supplement to the features in <numeric>, cxxomfort provides a number of facilities of its own in <cxxomfort/library/numeric.hpp>.

Supplementals for <random>

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

Supplementals for <string>

As a supplement to the features that cxxomfort backports, cxxomfort provides a number of facilities of its own in <cxxomfort/library/string.hpp>, including some pxxxxrx Proposals to Standard.

Supplementals for <tuple>

As a supplement to the features in <tuple>, cxxomfort provides a number of facilities of its own in <cxxomfort/library/tuple.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,...>).
  • function2tuple_t - the inverse of the above.

Supplementals for <type_traits>

As a supplement to the features in <type_traits>, cxxomfort provides a number of facilities of its own in <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>

As a supplement to the features that cxxomfort backports, cxxomfort provides a number of facilities of its own in <cxxomfort/library/utility.hpp>, including some pxxxxrx Proposals to Standard.

Other features:

  • function_arity - returns the number of arguments of a named function or object.

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.

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