optional<T> in a possible C++20 future

template<typename _Tp>
class optional
: private _Optional_base<_Tp>,
private _Enable_copy_move<
// Copy constructor.
is_copy_constructible<_Tp>::value,
// Copy assignment.
__and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
// Move constructor.
is_move_constructible<_Tp>::value,
// Move assignment.
__and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
// Unique tag type.
optional<_Tp>>
{
/* ... */
};
template <typename T>
class optional {
struct empty { };
union {
empty _;
T val_;
};
bool engaged_;
public:
optional(optional const& ) requires is_trivially_copy_constructible_v<T> && is_copy_constructible_v<T> = default;
optional(optional const& rhs) requires is_copy_constructible_v<T>
: engaged_(rhs.engaged_)
{
if (engaged_) {
new (val_) T(rhs.val_);
}
}
};

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Directly customising your qNFT experience

What is CSS Flexbox?

Step by Step Implement Active Directory Auth with Devise

Golang Database Integration Test with Github Action

Creating A Loading Scene in Unity

Visualizing SPARQL queries with PowerBI and Virtuoso

The Hottest New Releases — Retrieve and Relate — Out Now!

Elasticsearch- Introduction,Basic Concepts,Features & implementation

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Barry Revzin

Barry Revzin

More from Medium

Famous C++ Program that Every Programmer Should Know

How long Does It Take To Learn C++ Programming

Move-in-move-out in C++

Virtual Functions in C++