Home
PLF C++ Library - Tools
Introduction
These are some tools for using C++14/17/20 features in pre-C++14/17/20 compilation, as well as some PLF-specific functions/objects.
The tools are divided into 4 toolsets. Each toolset is #ifdef-guarded such that it will not be defined in a project more than once.
The first toolset, compiler defines, are assessments of availability of C++ features for most compilers (but mainly GCC/Clang/MSVC), resulting in defined macros for those features which are available. These are always instantiated, since they are used in the other 3 toolsets. An additional header, plf_tools_undef.h, undefines these macros - this can be used as needed at the end of a given project, however you should guard this with a per-file macro within your code if you're likely to have a class containing another class which also uses the tools eg. plf::list<plf::colony<int>>.
The other three toolsets are only instantiated if their matching macro is defined. These are:
- plf_tools - various tools. Instantiated if the macro PLF_INCLUDE_TOOLS is defined prior to including the .h.
- plf_bit_tools - bit-manipulation tools. Instantiated if the macro PLF_INCLUDE_BIT_TOOLS is defined prior to including the .h.
- plf_uninitialized_tools - allocator-aware versions of std::uninitialized_copy, std::uninitialized_move and std::uninitialized_fill_n. Instantiated if the macro PLF_INCLUDE_UNINITIALIZED_TOOLS is defined prior to including the .h.
The _INCLUDE macros are automatically undefined at the end of the .h, you do not need to undefine them manually.
Tool group details
Compiler defines
The macros are:
- PLF_DEFAULT_SUPPORT
- PLF_ALIGNMENT_SUPPORT
- PLF_INITIALIZER_LIST_SUPPORT
- PLF_TYPE_TRAITS_SUPPORT
- PLF_IS_ALWAYS_EQUAL_SUPPORT
- PLF_ALLOCATOR_TRAITS_SUPPORT
- PLF_VARIADICS_SUPPORT
- PLF_MOVE_SEMANTICS_SUPPORT
- PLF_NOEXCEPT
- PLF_NOEXCEPT_ALLOCATOR
- PLF_NOEXCEPT_SWAP
- PLF_NOEXCEPT_MOVE_ASSIGN
- PLF_CONSTEXPR
- PLF_CONSTFUNC
- PLF_CPP20_SUPPORT
- PLF_EXCEPTIONS_SUPPORT
- PLF_CPP11_SUPPORT
- PLF_CONSTEVAL_SUPPORT
- PLF_VOIDT_SUPPORT
See the header file for more information on all of these definitions and what they represent. The _noexcept, _constexpr and _constfunc macros are always defined, only their value changes. The _support macros are either defined or undefined.
In addition some supplementary macros are defined for construction/etc of objects using either std::allocator or std::allocator_traits, and with or without variadic support, based on the C++ version:
- PLF_CONSTRUCT
- PLF_CONSTRUCT_ELEMENT
- PLF_DESTROY
- PLF_ALLOCATE
- PLF_DEALLOCATE
PLF tools (define PLF_INCLUDE_TOOLS to use)
- plf::enable_if - std:: equivalent
- plf::conditional - std:: equivalent
- plf::less (function object) - std:: equivalent
- plf::equal_to (function object) - std:: equivalent
- plf::make_move_iterator (returns a std::move_iterator, C++11-and-above only) - std:: equivalent
- plf::void_cast - take any pointer, find the underlying address, cast it to void* and return it.
PLF bit tools (define PLF_INCLUDE_BIT_TOOLS to use)
- plf::popcount - std:: equivalent
- plf::countr_zero
- plf::countr_one
- plf::countl_zero
- plf::countl_one
The functions above are equivalents to their C++20 counterparts and will use the std:: versions internally if C++20/above is available, however countr/countl specifically do not check whether the supplied number is zero if the underlying platform only supports BSR instructions (only very old CPUs). This is intentional as the projects I use these functions in never supply zero.
PLF uninitialized tools (define PLF_INCLUDE_UNINITIALIZED_TOOLS to use)
- plf::uninitialized_copy(iterator_type begin, const iterator_type end, iterator_type destination, [[maybe_unused]] allocator_type &alloc)
- plf::uninitialized_move(iterator_type begin, const iterator_type end, iterator_type destination, [[maybe_unused]] allocator_type &alloc)
- plf::uninitialized_fill_n(iterator_type begin, std::size_t size, const element_type &element, [[maybe_unused]] allocator_type &alloc)
The above take an allocator, however if the allocator is determined to not have it's own custom construct function, the functions will use std::uninitialized_copy/move/fill_n internally. This means that if the allocator has a custom construct, it gets used and anything using these functions remains allocator-aware. If it doesn't, we get to use the internal optimizations typically-available in std::uninitialized_copy/move/fill_n (eg. for scalar types, fill_n may use memset or AVX fills). uninitialized_fill is not supplied as I don't need it. These are useful for using the functions with allocator-aware containers.
License
These tools are under a permissive zlib license. This means:
Software is used on 'as-is' basis. It can be modified and used in commercial
software. Authors are not liable for any damages arising from its use. The
distribution of a modified version of the software is subject to the following
restrictions:
- The authorship of the original software must not be misrepresented,
- Altered source versions must not be misrepresented as being the original
software, and
- The license notice must not be removed from source distributions.
Download
Download here or view
the repository
The library is a simple .h header file, to be included with a #include
command. Optionally the plf_tools_undef.h file can be included at the end of a project to undefine the compiler support macros.
Version history
- 2026-10-11: v1.0.11 - Fixes to bit manipulation tools from Michiel Von Slobbe. Restored decltype functionality for plf::indiesort.
- 2026-10-06: v1.0.9 - Correction to [[assume]] support. Adjustment to from_range_t and from_range structs.
- 2026-10-03: v1.0.8 - Add CPP11_SUPPORT for non-GCC compilers using libstdc++. Add PLF_64BIT_SUPPORT and bits_to_chars function from Michiel van Slobbe.
- 2026-09-20: v1.0.5 - Change PLF_CONSTRUCT_ELEMENT macro to be more emplace-friendly.
- 2026-09-19: v1.0.4 - Change version history to vcpkg-friendly format. Include memory.h for allocator/allocator_traits support under compiler defines, also because without it several library-specific macros won't be pulled in and compiler feature detection will not function correctly.
- 2026-09-10: v1.03 - Corrections for clang.
- 2026-07-31: v1.02 - Minor warning correction for plf_tools.h under MSVC.
- 2026-07-08: v1.01 - Minor correction.
- 2026-07-05: v1.00 - First release.
Contact:
plf:: library and this page Copyright (c) 2026, Matthew Bentley