fix a bug of any

This commit is contained in:
blueloveTH 2024-05-08 20:40:27 +08:00
parent 182177482f
commit 89fa6c9d3f

View File

@ -14,7 +14,7 @@ struct any{
inline static vtable* get(){ inline static vtable* get(){
static_assert(std::is_same_v<T, std::decay_t<T>>); static_assert(std::is_same_v<T, std::decay_t<T>>);
if constexpr (is_sso_v<T>){ if constexpr (is_sso_v<T>){
static vtable vt{ typeid(T), [](void*){} }; static vtable vt{ typeid(T), nullptr };
return &vt; return &vt;
}else{ }else{
static vtable vt{ typeid(T), [](void* ptr){ delete static_cast<T*>(ptr); } }; static vtable vt{ typeid(T), [](void* ptr){ delete static_cast<T*>(ptr); } };
@ -28,7 +28,7 @@ struct any{
any() : data(nullptr), _vt(nullptr) {} any() : data(nullptr), _vt(nullptr) {}
explicit operator bool() const { return data != nullptr; } explicit operator bool() const { return _vt != nullptr; }
template<typename T> template<typename T>
any(T&& value){ any(T&& value){
@ -52,7 +52,7 @@ struct any{
any(const any& other) = delete; any(const any& other) = delete;
any& operator=(const any& other) = delete; any& operator=(const any& other) = delete;
~any() { if(data) _vt->deleter(data); } ~any() { if(_vt && _vt->deleter) _vt->deleter(data); }
template<typename T> template<typename T>
T& _cast() const noexcept{ T& _cast() const noexcept{