home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,266 of 33,346   
   Johannes Schaub to All   
   Re: Explicit destructor call - problem w   
   09 May 12 15:04:26   
   
   50876cc6   
   From: schaub.johannes@googlemail.com   
      
   Am 09.05.2012 21:26, schrieb PiotrN:   
   > Hello,   
   >   
   > Two questions:   
   >     1)  Why I cannot call destructor for builtin types like int:   
   >             int a;   
   >             int* p = new (&a) int(7);   
   >             p->~int();   
   >   
   > I found in the NET that standard says: "The notation for explicit call   
   > of a destructor can be used for any scalar type name. Allowing this   
   > makes it possible to write code without having to know if a destructor   
   > exists for a given type. "   
   > And typedef int is "scalar type name" but int is not - so it works the   
   > following:   
   >           typedef int INT;   
   >           p->~INT();   
   > Why standard does allow calling destructor on typedef (and template   
   > typename) - but it has objections to plain type? Is there any   
   > rationale for this?   
   >   
      
   While I don't know the rationale, I think it's likely that it's because   
   plain "~int()" is not very useful (at least not as long as it is a   
   "noop", like currently).   
      
   The utility of a pseudo destructor call like "p->~T()" with T being   
   "int" is that you can say "p->~T()" in a *template* disregarding of   
   whether "T" actually is a class or not. In these cases you use a   
   template parameter which is a "type-name".   
      
      
   >    2) Why C++ (gcc-4.3.4) has objections to this syntax:   
   >   
   >       p->std::~string();   
   >   
      
   Because the syntax is "p->~ typename", so you need to say   
      
       p->~std::string();   
      
   Note that in this call, this actually calls the destructor. It is not a   
   pseudo destructor call like for the "int" case and hence it is not a noop.   
      
   > of course this works well:   
   >   
   >     using namespace std;   
   >     p->~string();   
   >   
   >   
      
   This works as long as you have the "using namespace std;" or a "using   
   std::string;" in place. If you haven't, then "string" is not known as a   
   class name and you either need to use the above, or the below   
   (std::string is just a typedef for a particular "basic_string" template   
   specialization having "char" as element type).   
      
       p->~basic_string();   
      
   This will find the implicitly declared "basic_string" type name in the   
   std::string class (if you say "p->~foo", then "foo" is searched both in   
   the current scope and in the scope of "p") and is equivalent to   
   "p->~std::string();", as far as the effects are concerned.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca