Last active
May 14, 2023 22:07
-
-
Save m5knt/0fcd120f1dc96631e7da4ba95f544d7c to your computer and use it in GitHub Desktop.
C++ Property, implement with EBO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <type_traits> | |
#include <string> | |
template<class T> | |
struct PropertyUtil { | |
// empty base | |
PropertyUtil(const PropertyUtil&) = delete; | |
PropertyUtil(PropertyUtil&&) = delete; | |
PropertyUtil& operator=(const PropertyUtil&) = delete; | |
PropertyUtil& operator=(PropertyUtil&&) = delete; | |
protected: | |
PropertyUtil() {} | |
constexpr auto operator->() noexcept -> T* { | |
return reinterpret_cast<T*>(this); | |
} | |
constexpr auto operator->() const noexcept -> const T* { | |
return reinterpret_cast<const T*>(this); | |
} | |
}; | |
template<typename T> | |
struct FooProperty { | |
// empty base | |
FooProperty() {} | |
FooProperty(const FooProperty&) {}; | |
FooProperty(FooProperty&&) {}; | |
FooProperty& operator=(const FooProperty&) {}; | |
FooProperty& operator=(FooProperty&&) {}; | |
[[no_unique_address]] struct : PropertyUtil<T> { | |
operator int() noexcept { | |
return this[0]->value_; | |
} | |
auto operator=(int n) noexcept -> int { | |
this[0]->value_ = n; | |
return n; | |
} | |
} value; | |
}; | |
struct Dummy {}; | |
static_assert(std::is_empty<FooProperty<Dummy>>::value); | |
struct Foo : FooProperty<Foo> { | |
private: | |
int value_ = 123; | |
friend struct FooProperty<Foo>; | |
friend int main(); | |
}; | |
int main() | |
{ | |
auto a = Foo(); | |
//auto _ = a; | |
std::cout << a.value << std::endl; | |
a.value = 256; | |
std::cout << a.value << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment