2

Is it possible to call destructor(without operator delete) using decltype and\or std::remove_reference? Here's an example:

#include <iostream>
#include <type_traits>

using namespace std;

class Test
{
    public:
    Test() {}
    virtual ~Test() {}
};

int main()
{
    Test *ptr;

    ptr->~Test(); // works
    ptr->~decltype(*ptr)(); // doesn't work
    ptr->~std::remove_reference<decltype(*ptr)>::type(); // doesn't work

return 0;
}
4

3 回答 3

5

You can use an alias template to get an unqualified type name when all you have is a qualified type name. The following should work

template<typename T> using alias = T;
ptr->~alias<std::remove_reference<decltype(*ptr)>::type>();

Note that if the remove_reference thing worked, it would still be dangerous, because by the qualified type name, you would inhibit an virtual destructor call. By using the alias template, virtual destructors still work.

Note that GCC4.8 appears to accept

ptr->std::remove_reference<decltype(*ptr)>::type::~type();

Clang rejects this. I have long given up trying to understand how destructor name lookup works (if you look into the clang source, you will note that the clang developers also do not follow the spec, because they say that it makes no sense here). There exist DRs that cover destructor call syntax and how they are messed up. Therefor, I would recommend not using any complicated syntax here.

于 2013-06-26T19:30:13.997 回答
1

如果您的编译器不支持使用命令的模板,您可以执行以下操作:

定义模板结构:

template<class T> struct unwind_alias { static VOID destroy(T* ptr) { ptr->~T(); }; };

用它来破坏对象

unwind_alias<std::remove_reference<decltype(*ptr)>::type>::destroy(ptr);

希望它会帮助任何人。

于 2013-09-21T12:14:38.830 回答
1

使用 C++17,您也可以只使用std::destroy_at.

于 2018-10-29T06:57:29.663 回答