cxxomfort  rel.20211024
Simple backports for C++ - https://ryan.gulix.cl/fossil.cgi/cxxomfort/
<memory>

Backports related to Standard Header <memory>

// or as part of:

Interfaces

Interfaces defined here:

Interfaces repointed here when found in TR1:

Non-backport interfaces (cxxomfort::fix):

Smart Pointer accessors

cxxomfort provides, for the available smart pointer types, the special function cxxomfort::fix::to_bool() which takes as argument a smart pointer type and returns if it currently manages a value, as if checking their (C++>=11) operator bool property.

shared_ptr<int> P1 = new int(10);
unique_ptr<int> P2 = new int(20);
if (to_bool(P1)) {
...
}
if (to_bool(P2)) {
...
}

Functionally, invoking these accessors is a simpler means to checking the managed status than using eg.: explicit_cast<bool>(...), and it has the advantage of working in C++03 and C++11/14 pre the new "@c if condition declaration" rule as well:

// does not work pre-C++17:
shared_ptr<Foo> managedfoo = make_shared<Foo>(args...);
// this fails: no implicit conversion of managed pointer to bool
if (bool f = (managedfoo)) {
...code
}
// works in all versions of C++, no extra cost:
shared_ptr<Foo> managedfoo = make_shared<Foo>(args...);
if (bool f = to_bool(managedfoo)) {
...code
}
// note that this is basically equivalent to the following
// in all versions (maps to static_cast in > C++11):
shared_ptr<Foo> managedfoo = make_shared<Foo>(args...);
if (bool f = explicit_cast<bool>(managedfoo)) {
...code
}

Notes

Note
Cxxomfort expects that the compiler provides an alignment information macro such as __alignof(T).
Cxxomfort expects an implementation of std::shared_ptr to exist and work in order, as part of the general TR1 requirement.
Warning
Before C++11, aligned_storage<S,A> may not deliver a type with correct alignment when the template argument A is ommitted (in which case, a likely result is a type aligned to sizeof(int)).
For production purposes, A always should be given explicitly, eg.: with the usual pattern of
aligned_storage<sizeof(T),alignof(T)>.

See Also

See also
header/memory @ cppreference
memory/unique_ptr @ cppreference
Member cxxomfort::cxxostd::destroy (Iter ini, Iter2 fin)
Member cxxomfort::cxxostd::destroy_at (T *p) CXXO_NOEXCEPTNOTHROW

Member cxxomfort::cxxostd::destroy_n (Iter ini, Integer n)
Member cxxomfort::cxxostd::uninitialized_default_construct (It ini, It fin)
Member cxxomfort::cxxostd::uninitialized_default_construct_n (It ini, Integer n)
Class malloc_deleter
File memory.hpp