Examples for using the cxxomfort backports library for C++ (http://ryan.gulix.cl/fossil.cgi/cxxomfort/) and its features.

Luis Machuca Bezzaza eed2bd04d1 Updated examples for cxxomfort 202008xx. 1 month ago
examples eed2bd04d1 Updated examples for cxxomfort 202008xx. 1 month ago
LICENSE 7273357b4b Initial commit 4 years ago
README.md 74f3a784cc README update, tests with MSVC 2012 compat 3 months ago



Examples for using the cxxomfort backports library

This is a set of usage examples for cxxomfort, the backports library for C++, intended both as a demonstration of the working features and as a provision of code samples when pointing users to the library.


Have the cxxomfort library (version >= 20200101) installed. Pick the example you want and compile.

In order to work correctly, you can do any of the following:

  • Copy or link the cxxomfort/ directory of the distribution into your compiler's system search path (/usr/local/include/ or similar).
  • Add the path with -I /path/to/cxxomfort, or the equivalent option for your compiler / project.

Current Examples

1.- Showing information about cxxomfort and support.

  • 0info.cpp - shows basic information, in particular detected compiler setup.
  • 0showsupport.cpp - besides the above, shows specific information about features detected and supported by the library.

2.- Basic demonstrations - show off one feature at a time.

  • byte.cpp - C++17's std::byte backport.
  • explicit_cast.cpp - explicit_cast and explicit conversion operator emulation.
  • gcd.cpp - gcd and the pre-C++14 static version static_gcd part of cxxomfort:library.
  • generic_errors.cpp - showcases the C++11 <system_error> features.
  • randonly.cpp - example use of utilities related to <random> as well as iterator accessors.
  • plus.cpp - backports of C++14's "heterogeneous functors" (plus<void> and the like), as well as foreach emulation.
  • to_string.cpp - C++11's to_string backport.
  • tuple_get.cpp - tuple<type> access elements by type, as well as testing features such as static_assert.

3.- Showcasing libraries and supplementals.

  • foreach.cpp - foreach loop emulation feature (equivalent to for (v : sequence) { ... }), part of cxxomfort:library; among other things.
  • to_string-improved.cpp - extended to_string proposal (p0117r0) in cxxomfort:library.
  • type_name.cpp cxxomfort:library's typeid_demangle and type_name<> utilities.
  • typesafe_enum.cpp showcases the typesafe enum implementation, an alternative to C++11's enum class.

4.- Examples adapted from external sources.

The examples in examples/cppref and examples/cplusplus have been adapted from the cppreference and cplusplus sites for use with cxxomfort.

  • invoke.cpp - the std::invoke example lifted from the cppreference wiki.
  • typeindex.cpp - std::type_index and its use in hashes.
  • unique_ptr.cpp - the unique_ptr<> example lifted from cppreference / cplusplus.


Testing Environments

Unless specified otherwise in any of the source files, these examples have been tested in most or all of the following environments:

  • GCC 4.6 without --std=c++0x
  • GCC 4.6 with --std=c++0x.
  • MSVC 2008 Express.
  • MSVC 2010 Express.

GCC limitations

GCC in C++03 mode does not allow for adding tr1/ to the compiler search path due to a bug. A TR1 forwarder has to be used according to the cxxomfort documentation; the examples here are compiled using tr1_fwd.

For more information read cxxomfort configuration.

MSVC limitations

MSVC 2008 Express does not include a working TR1 in its default install. Other than providing your own via eg.: Boost.TR1 or nononsenseboost.TR1, the best option is to install MSVC 2008 Express SP1.

For more information read cxxomfort configuration.