[cfe-dev] error: destructor reference must be called immediately with '()' (Why dtor is an exception?)

David Abdurachmanov david.abdurachmanov at gmail.com
Wed Oct 1 05:22:04 PDT 2014


Hi cfe-dev,

After moving to LLVM/Clang 3.5 I have noticed some failures, seems to be coming from the following (5 years old) commit: https://github.com/llvm-mirror/clang/commit/a78c5c34fbd20fde02261c3f3e21933cd58fcc04

We started getting these kind of errors:

AnalysisDataFormatsTopObjects/a/xr.cc:915:2: error: destructor reference must be called immediately with '()'
(((::TtDilepEvtSolution*)o)->::TtDilepEvtSolution::~TtDilepEvtSolution)();
 ^
                                                    ()
AnalysisDataFormatsTopObjects/a/xr.cc:915:72: error: called object type 'void' is not a function or function pointer

Small example below. Why is that only after dtor you **require** next token to be '('? Yet it's fully fine to add as many extra parentheses in other cases (see Point::dump below).

Comment from commit: The only way a reference to a destructor can be used is to immediately call them. Since the next token is not a '(', produce a diagnostic and build the call now.

$ cat dtor.cc
#include <new>
#include <cstdlib>
#include <iostream>

class Point
{
  public:
    Point(int x, int y) : x(x), y(y) { std::cerr << "ctor " << x << " " << y << std::endl; }
    ~Point() { std::cerr << "dtor" << std::endl; }
    void dump() { std::cerr << "x: " << x << "; y:" << y << std::endl; }
  private:
    int x, y;
};

int main(int argc, char *argv[])
{
  char *mem = (char *)malloc(100);
  Point *pt = new (mem) Point(10, 20);
  (pt->Point::dump)();
  (((pt->Point::dump)))();
  (pt->Point::~Point)();
}

$ clang++ dtor.cc
dtor.cc:20:4: error: destructor reference must be called immediately with '()'
  (pt->Point::~Point)();
   ^
               ()
dtor.cc:20:22: error: called object type 'void' is not a function or function pointer
  (pt->Point::~Point)();
  ~~~~~~~~~~~~~~~~~~~^
2 errors generated.

david
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20141001/5170d64a/attachment.sig>


More information about the cfe-dev mailing list