A collection of standalone C++ utilities.
- state_saver - scope guards for C++11 and later that restore a saved value on exit, failure, or success.
- attributes - portable attribute macros for C++98 and later.
- unused - helpers for suppressing unused-variable warnings in C++11 and later.
- concepts - SFINAE-friendly aliases for C++11 and later.
- type_traits - additional type traits for C++11 and later.
- byte - a distinct byte type and byte-copy helpers for C++17 and later.
- utility - general utility helpers for C++17 and later.
A combined nstd_example.cpp demonstrates several modules together.
attributes provides portability macros for common compiler and standard attributes. Unsupported attributes use a safe fallback.
ATTR_NORETURN- marks a function that does not return.ATTR_ALWAYS_INLINE- requests function inlining.ATTR_DEPRECATED("reason")- marks a declaration as deprecated.ATTR_NODISCARD- asks the compiler to warn when a return value is discarded.ATTR_NODISCARD_MSG("reason")- same asATTR_NODISCARD, with a diagnostic reason when supported.ATTR_MAYBE_UNUSED- suppresses unused warnings on declarations.ATTR_TRIVIAL_ABI- requests Clang'strivial_abifor eligible classes. Put it afterclassorstruct, for examplestruct ATTR_TRIVIAL_ABI type.ATTR_NO_UNIQUE_ADDRESS- allows a non-static data member to share an address when supported.
ATTR_ASSUME(expr)- tells the optimizer thatexpris true. Do not use it for validation or pass expressions with side effects.ATTR_FALLTHROUGH- marks an intentionalswitchfallthrough.ATTR_LIKELY(expr)/ATTR_UNLIKELY(expr)- mark likely and unlikely branch conditions.
See attributes_example.cpp for a complete example.
unused provides local-scope helpers for intentionally unused variables and parameters.
nstd::unused(args...)- evaluates its arguments and binds them by reference.NSTD_UNUSED(args...)- macro helper for one or more variables and parameters. Its arguments are not evaluated.
Use ATTR_MAYBE_UNUSED from attributes for declaration-level suppression and NSTD_UNUSED for local expression-level suppression.
See unused_example.cpp for a complete example.
byte provides a C++17 std::byte-style type in the nstd namespace. It is not arithmetic and does not alias char.
nstd::byte- scoped enum backed byunsigned char.nstd::to_byte(value)- converts an integral value tonstd::byte.nstd::to_integer<T>(byte)- converts a byte to an integral type.- Bitwise and shift operators are supported. Shift counts must be non-negative integral values other than
booland smaller than the bit width ofunsigned int. nstd::to_bytesandnstd::from_bytescopy trusted object representations of trivially copyable types; they do not validate serialized or untrusted input.- The return-by-value
nstd::from_bytes<T>also requires a default constructor and a trivial copy or move constructor; the overload that writes to an existing object does not. - Counted copies support overlap and treat zero count as a no-op, including null pointers. Non-empty copies require valid pointers and sufficiently large buffers; overflowing counts are rejected without copying.
See byte_example.cpp for a complete example.
concepts provides C++11 SFINAE aliases, not C++20 concept declarations. An alias resolves to the constrained type when its predicate is true.
#include <concepts.hpp>
template <typename T>
nstd::Integral<T> next_value(T value) {
return static_cast<T>(value + 1);
}nstd::EnableIf<C, T>- exposesTwhenCis true.nstd::TypeConcept<T, C>- exposesTwhenCis true.
nstd::RValue<T>/nstd::LValue<T>- forwarding-reference deduction result category.RValuerejects const rvalues.nstd::Reference<T>- lvalue or rvalue reference.nstd::Const<T>/nstd::NotConst<T>- top-level const qualification after reference removal.nstd::Volatile<T>/nstd::NotVolatile<T>- top-level volatile qualification after reference removal.nstd::Void<T>andnstd::NullPointer<T>.nstd::Arithmetic<T>,nstd::Integral<T>,nstd::SignedIntegral<T>,nstd::UnsignedIntegral<T>,nstd::FloatingPoint<T>.nstd::Enum<T>,nstd::Pointer<T>,nstd::MemberPointer<T>,nstd::Array<T>,nstd::Function<T>.nstd::Object<T>,nstd::Scalar<T>,nstd::Class<T>,nstd::Union<T>.nstd::Trivial<T>andnstd::TriviallyCopyable<T>.
nstd::Constructible<T, Args...>andnstd::NothrowConstructible<T, Args...>.nstd::DefaultConstructible<T>,nstd::CopyConstructible<T>,nstd::MoveConstructible<T>.nstd::Destructible<T>andnstd::NothrowDestructible<T>.nstd::Assignable<T, U>andnstd::NothrowAssignable<T, U>.nstd::CopyAssignable<T>andnstd::MoveAssignable<T>.nstd::Same<T, U>- exactly the same type.nstd::ConvertibleTo<T, U>-Tis convertible toU.
See concepts_example.cpp for a complete example.
type_traits provides C++11-compatible helpers for type transformation, detection, and a few missing standard-library traits.
Variable-template _v forms are available when supported by the selected language standard.
nstd::bool_constant<B>,nstd::enable_if_t<C, T>,nstd::conditional_t<C, T, F>.nstd::decay_t<T>,nstd::remove_const_t<T>,nstd::remove_volatile_t<T>,nstd::remove_cv_t<T>.nstd::remove_pointer_t<T>,nstd::remove_reference_t<T>,nstd::remove_extent_t<T>,nstd::remove_all_extents_t<T>.nstd::add_const_t<T>,nstd::add_volatile_t<T>,nstd::add_cv_t<T>.nstd::add_pointer_t<T>,nstd::add_lvalue_reference_t<T>,nstd::add_rvalue_reference_t<T>.nstd::make_signed_t<T>andnstd::make_unsigned_t<T>.nstd::underlying_type_t<T>.
nstd::void_t<T...>.nstd::is_detected<Op, Args...>/nstd::is_detected_v<Op, Args...>.nstd::detected_t<Op, Args...>,nstd::detected_or<Default, Op, Args...>, andnstd::detected_or_t<Default, Op, Args...>.nstd::is_detected_exact<Expected, Op, Args...>/nstd::is_detected_exact_v<Expected, Op, Args...>.nstd::is_detected_convertible<To, Op, Args...>/nstd::is_detected_convertible_v<To, Op, Args...>.
nstd::identity<T>/nstd::identity_t<T>andnstd::type_identity<T>/nstd::type_identity_t<T>.nstd::remove_ptr<T>/nstd::remove_ptr_t<T>.nstd::remove_ref<T>/nstd::remove_ref_t<T>.nstd::remove_cv_ref<T>/nstd::remove_cv_ref_t<T>andnstd::remove_cvref_t<T>.nstd::remove_all_ptr<T>/nstd::remove_all_ptr_t<T>.nstd::remove_all_cv_ref_ptr<T>/nstd::remove_all_cv_ref_ptr_t<T>.nstd::remove_all_cv_ref_ptr_ext<T>/nstd::remove_all_cv_ref_ptr_ext_t<T>.nstd::conjunction<T...>,nstd::disjunction<T...>, andnstd::negation<T>.nstd::is_same_signedness<T, U>- true when both types have the same signedness.nstd::is_nothrow_convertible<From, To>- C++11-compatible backport ofstd::is_nothrow_convertible.
See type_traits_example.cpp for a complete example.
state_saver restores a saved object value on exit, failure, or success.
#include <state_saver.hpp>
void use_temporary_value(int& value) {
SAVER_EXIT(value);
value = 42;
// value is restored when the scope exits, including exception unwinding.
}nstd::saver_exit<T>/SAVER_EXIT(x)- restores on scope exit.nstd::saver_fail<T>/SAVER_FAIL(x)- restores when scope exits while a new exception is being unwound.nstd::saver_success<T>/SAVER_SUCCESS(x)- restores when scope exits without a new exception being unwound.MAKE_SAVER_EXIT(name, x),MAKE_SAVER_FAIL(name, x), andMAKE_SAVER_SUCCESS(name, x)create named guards.WITH_SAVER_EXIT(x),WITH_SAVER_FAIL(x), andWITH_SAVER_SUCCESS(x)create a guard for a simple block. Use a named guard if the block needsbreakorcontinue.
See state_saver_exit_example.cpp, state_saver_fail_example.cpp, and state_saver_success_example.cpp for complete examples.
dismiss()disables automatic restore.restore()restores immediately. It does not dismiss the guard, so the destructor may restore again later unlessdismiss()is called.
The object must be non-const, non-pointer, non-array, non-function, constructible from a non-const lvalue, assignable, and nothrow destructible. Automatic restore assigns from an rvalue if that expression is noexcept or lvalue assignment is unavailable; otherwise it assigns from an lvalue. restore() assigns from an lvalue.
Set configuration macros before including <state_saver.hpp> and use the same values in every translation unit. At most one restore policy may be defined:
STATE_SAVER_MAY_THROW_RESTORE- restore may throw; this is the default.STATE_SAVER_NO_THROW_RESTORE- requires noexcept restore.STATE_SAVER_SUPPRESS_THROW_RESTORE- catches restore exceptions.
With the default policy, an automatic restore that throws during exception unwinding calls std::terminate. Use the no-throw policy for noexcept assignment or the suppress policy when this must be avoided.
Optional settings:
STATE_SAVER_CATCH_HANDLER- handles suppressed restore exceptions and must not throw.STATE_SAVER_NO_THROW_CONSTRUCTIBLE- requires nothrow saved-value construction.STATE_SAVER_FORCE_MOVE_ASSIGNABLE- forces automatic assignment from an rvalue.STATE_SAVER_FORCE_COPY_ASSIGNABLE- forces automatic assignment from an lvalue.
utility provides C++17 forwarding, conversion, invocation, and comparison helpers.
nstd::move(x)- move cast that rejects const objects.nstd::move_if_noexcept(x)- move-construction helper matchingstd::move_if_noexcept, with const-object rejection.nstd::move_assign_if_noexcept(x)- assignment-oriented equivalent that falls back toconst T&when move assignment may throw and copy assignment is available.nstd::forward<T>(x)- forwarding helper matchingstd::forward.nstd::forward_like<T>(x)- C++23-style helper that appliesT's const/reference qualifiers tox.nstd::unforward(x)andnstd::decay_copy(x)- value-materialization helpers.
nstd::bit_cast<To>(from)- C++17 cast between same-size trivially copyable types.Tomust be non-cv, have a default constructor and a trivial copy or move constructor, and accept the source representation. Padding bytes need not be preserved.nstd::invoke_each(f, args...)- invokesfonce for each argument.nstd::apply_each(f, tuple_like)- invokesfonce for each element of a tuple-like object such asstd::tuple,std::pair, orstd::array.nstd::to_underlying(e)- C++23-style enum-to-underlying conversion.nstd::cmp_equal,nstd::cmp_not_equal,nstd::cmp_less,nstd::cmp_greater,nstd::cmp_less_equal, andnstd::cmp_greater_equal- C++20-style signed/unsigned integer comparisons without lossy casts.nstd::in_range<T>(value)- C++20-style signed/unsigned integer range check.nstd::constexpr_for<Start, End, Inc>(f)- increasing loop over same-type non-bool integral constants.Incmust be positive.
See utility_example.cpp for a complete example.