C++ Logo

std-discussion

Advanced search

Re: Usage of destroying delete in CRTP base class?

From: Breno Guimarães <brenorg_at_[hidden]>
Date: Tue, 10 Oct 2023 07:29:52 -0300
Hi Peter,

What is the use of casting to the base class in this case? Since it's a
cast to Base<Derived>*, it doesn't give you any more generality than having
Derived* itself, right?

Thanks,
Breno G.

Em ter., 10 de out. de 2023 04:36, Peter Sommerlad (C++) via Std-Discussion
<std-discussion_at_[hidden]> escreveu:

> Hi
> did anybody use so far destroying delete in CRTP base class templates
> for correctly deleting via base pointer in C++20?
> See my example:
>
> https://godbolt.org/z/PWPxYdj5q
>
>
> ==========
> #include <iostream>
> #include <memory>
> #include <type_traits>
> template<typename TAG, typename destroyer_t>
> constexpr
> TAG *destroyer_cast(destroyer_t*ptr){
> static_assert(std::is_base_of_v<destroyer_t,TAG>);
> return static_cast<TAG*>(ptr);
> }
> template<typename derived>
> struct base {
> ~base() { std::cout << "base dtor \n";}
> // simulate mix-in function:
> friend void print(derived const& d){
> std::cout << typeid(decltype(d)).name()<<'\n';
>
> }
> // only available in C++20
> #ifdef __cpp_impl_destroying_delete
> void operator delete( base* ptr, std::destroying_delete_t )
> {
> // cannot check if TAG is subtype here directly,
> // because derived might be incomplete
> // therefore indirect the static_cast
> destroyer_cast<derived>(ptr)->~derived();
> ::operator delete((void*)ptr);
> }
> #endif
> };
>
> struct d: base<d> {
> using base = base<d>;
> ~d() { std::cout << "derived dtor\n";}
> };
>
>
> int main(){
> auto p = std::make_unique<d>();
> print(*p);
> std::unique_ptr<d::base> pb {std::move(p)};
> }
> ========
>
> --
> Peter Sommerlad
>
> Better Software: Consulting, Training, Reviews
> Modern, Safe & Agile C++
>
> peter.cpp_at_[hidden]
> +41 79 432 23 32
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

Received on 2023-10-10 10:30:04