Advertisement
Guest User

std::is_destructible with private destructors

a guest
Aug 15th, 2014
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. // Is the Standard's definition of std::is_destructible unclear?
  2. #include <cstdio>
  3. #include <type_traits>
  4. #include <utility>
  5.  
  6. struct PrivateDestructor
  7. {
  8. public:
  9.     void *TestFunction();
  10.     void Destroy() { delete this; }
  11. private:
  12.     ~PrivateDestructor() { }
  13. };
  14.  
  15. void *PrivateDestructor::TestFunction()
  16. {
  17.     typedef PrivateDestructor U;
  18.     // Expression from the Standard is well-formed (evaluates here to "void")...
  19.     decltype(std::declval<U&>().~U()) *ptr = this;
  20.     return ptr;
  21. }
  22.  
  23. int main()
  24. {
  25.     // ...yet this is false in GCC and Clang.  Is the Standard unclear?
  26.     std::printf("%d\n", std::is_destructible<PrivateDestructor>::value);
  27.    
  28.     PrivateDestructor *obj = new PrivateDestructor();
  29.     std::printf("%p\n", obj->TestFunction());
  30.     obj->Destroy();
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement