Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Artifact ID: | 78a3f2b6afa1f8ab1a2eb9ea6863bafb6fdebd79 |
---|---|
Page Name: | Features |
Date: | 2018-08-27 15:53:41 |
Original User: | luismachuca |
Parent: | 009cf59aa74fd74e41b224405b4c149f07681cae (diff) |
Next | 40a27c05d798dfc513d1e915d6086d8173f801cac99b7375d761a79395da8732 |
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:
- Integrated Type Traits for inspection of native types.
- List of C++11 keywords added by this library.
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>
)
nullptr
aka null pointer literal.static_assert
, compile-time assertions.- Iterator Helpers such as global begin, end.
explicit_cast
/ explicit operator conversion backports.- Metaprogramming Helpers such as enable_if.
- Rvalue references, move and forward emulation.
- Extended integral types, aka uint16_t etc.
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)
- Initialization of Sequences and Containers
- Features from <algorithm>: new copy, sort, shuffle algorithms.
- Features from <cstddef>: nullptr_t, byte, etc.
- Features from <cstdint>: typedefs for integral types.
- Features from <forward_list>, as in specifically
std::forward_list
. - Features from <functional>, such as transparent functors.
- Features from <limits>, in particular constants for min/max.
- Features from <memory>, such as
alignof
and very especially "unique_ptr
" for dynamic management of objects. - Features from <random>: C++03 renames of the C++11 random utilities.
- Features from <string>, in particular
to_string
. - Features from <tuple>, in particular tuple "apply".
- Features from <type traits> (partial implementation).
- Features from <utility>, in particular declval.
- Other standard features such as algorithms and utility functions not in their own section.
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
. See also: https://en.cppreference.com/w/cpp/iterator .
The following interfaces are backported from C++11:
std::begin
andstd::end
-- generically find the begin and end iterators for a container-like expression.std::next
andstd::prev
-- generically advance an iterator in forward or backward direction.
The following interfaces are backported from 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.
The following interfaces are backported from C++17 (but they have a wider meaning and support):
size::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 "is_sorted" algorithm.
- The "is_permutation" algorithm.
- The new C++14 overloads for
equal
algorithm, specific to different-sized sequences. - The new C++17
clamp
algorithm. (External Documentation) - The "for_each_n" algorithm from C++17: https://en.cppreference.com/w/cpp/algorithm/for_each_n
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 <cstddef>
To enable these features, #include
the cxxomfort/cstddef.hpp header. Or just include cxxomfort/cxxomfort.hpp and it will be added automatically.
As a convenience for portability, the <cstddef> header is also provided separately.
This enables access to a number of features from C++ that are declared in the <cstddef>
header:
nullptr_t
from C++11 (see nullptr above).std::max_align_t
from C++11.std::byte
from C++17 (see std::byte at cppreference and Features/std::byte for more information).
Features from <cstdint>
To enable these features, #include
the cxxomfort/cstdint.hpp header. Or just include cxxomfort/cxxomfort.hpp and it will be added automatically.
As a convenience for portability, the <cstdint> header is also provided separately.
This enables access to a number of features from C++11 onwards, specifically the typedefs for integral types such as uint16_t, int64_t
, etc.
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
andemplace_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
andbit_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 header simply redirects to the base.hpp headers, as the iterator-related features form part of the base cxxomfort package. 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:
addressof
from C++11.- A "
alignof(T)
" macro that uses compiler specific to backportalignof
keyword (in C++03 compilers, usually something like__alignof
). - A "
alignas
" macro that uses compiler specifics to backportalignas
keyword. - A partial implementation of C++11's
aligned_storage
, depending on the two above features. - A barebones implementation of
pointer_traits
and ofallocator_traits
to ease uniform API usage when writing eg.: containers. - A backport of
unique_ptr
for C++03, explained in its own section below. make_unique
from C++14.
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 <numeric>
To enable these features, #include
the cxxomfort/numeric.hpp header.
The following interfaces from more advanced C++ versions are backported to previous versions:
iota
from C++11.
The following interfaces are also added:
- A complimentary
iota_n
for symmetry with copy_n, for_each_n, etc.
Features from <random>
To enable these features, #include
the cxxomfort/random.hpp header. Or just include cxxomfort/cxxomfort.hpp and it will be added automatically.
As a convenience for portability, the <random> header is also provided separately.
This enables access to a number of features from later C++11 additions in C++03:
- Renames of the random utilities in
<tr1/random>
that changed name in C++11 onward, for examplestd::tr1::uniform_int_distribution
forstd::tr1::uniform_int
.
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 along
numerical expression. strto(u)ll
in versions of MSVC where it's not available under that name.
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 more advanced C++ versions are backported here:
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: cppreference documentation).make_from_tuple<T>
from C++17 - create an object using a tuple as constructor arguments (see:cppreference documentation).
Features from <type_traits>
Various interfaces from C++'s <type_traits>
header are ported here to previous versions:
is_lvalue_reference
andis_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.
void_t
,make_void
from C++17 to previous Standards.type_identity
from C++17. ref.bool_constant
from C++17. ref.endian
for endianness detection, from C++17. https://en.cppreference.com/w/cpp/types/endian|ref].- Others.
TO BE ORGANIZED
Features from <utility>
The following interfaces are brought in from their C++ versions to previous standards:
declval
to provide pseudovariables in unevaluated contexts, from C++11.exchange
for pushing-in new values for variables, from C++14.as_const
helper for const_casts, from C++17.
In C++03 mode, declval
returns lvalue-references.
For C++03 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 only:
- An implementation of
integer_sequence
andindex_sequence
.