Skip to content

Instantly share code, notes, and snippets.

@ZhekehZ
Created August 28, 2020 14:15
Show Gist options
  • Save ZhekehZ/cfe805d33f30e3c4c0133a0f426d7ed3 to your computer and use it in GitHub Desktop.
Save ZhekehZ/cfe805d33f30e3c4c0133a0f426d7ed3 to your computer and use it in GitHub Desktop.
C++ linear types by Ivan Cukic from Meeting C++ 2018 Secret Lightning talks
#include <type_traits>
#include <utility>
namespace detail {
template <typename T, typename U>
constexpr bool linear_usable_as_v =
std::is_nothrow_constructible_v<T, U> &&
std::is_nothrow_assignable_v<T&, U> &&
std::is_nothrow_convertible_v<U, T>;
template <typename T, typename U>
constexpr bool linear_unusible_as_v =
!std::is_constructible_v<T, U> &&
!std::is_assignable_v<T&, U> &&
!std::is_convertible_v<U, T>;
}
template <typename T>
concept Linear =
std::is_nothrow_destructible_v<T> &&
detail::linear_usable_as_v<T, T> &&
detail::linear_usable_as_v<T, T&&> &&
detail::linear_unusible_as_v<T, T&> &&
detail::linear_unusible_as_v<T, T const &> &&
detail::linear_unusible_as_v<T, const T>;
template <typename T>
class linear_wrapper {
public:
linear_wrapper(T && value) : value_(value) {}
linear_wrapper(linear_wrapper const &) = delete;
linear_wrapper(linear_wrapper &&) noexcept = default;
linear_wrapper& operator=(linear_wrapper const &) = delete;
linear_wrapper& operator=(linear_wrapper &&) noexcept = default;
[[nodiscard]] T&& get() && noexcept { return std::move(value_); }
private:
T value_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment