[cfe-dev] Clang not generating the destructor

Riyaz Puthiyapurayil via cfe-dev cfe-dev at lists.llvm.org
Wed Apr 28 18:55:21 PDT 2021


The following test case fails to compile with clang++ because it does not generate code for a destructor (as indicated below). Is this a bug or am I missing something? Gcc works fine.

Wandbox permlink: https://wandbox.org/permlink/H7BcjQzXwoqZclj3


// prog.cpp
#include "D.h"

D *f(char c) {
  return c ? new D(c) : new D(-1);
}

D *x;

int main() {
  x = f('a');
  return 0;
}

// D.h
#ifndef D_H
#define D_H

extern void unknown();

template<typename T>
class C {
  T t;
public:
  explicit C(T t_) : t(t_) {}
  ~C() {}
};

class D {
  union U
  {
    explicit U(int i)  : ci(i) { unknown(); }
    explicit U(char c) : cc(c) {}
    ~U() {}
    C<int>  ci;    // destructor C<int>::~C() not generated
    C<char> cc;
  };

  U u;
  bool uIsInt;

public:
  explicit D(int i)  : u(i), uIsInt(true)  {}
  explicit D(char c) : u(c), uIsInt(false) {}
  ~D(); // explicit destruction of u
};
D* f(char c);
#endif // D_H

// unk.cpp
#include "D.h"

void unknown() {}

D::~D() {
  if (uIsInt)
    u.ci.~C();
  else
    u.cc.~C();
}

$ clang++ prog.cpp unk.cpp
/tmp/prog-bfa5e6.o: In function `D::U::U(int)':
prog.cc:(.text._ZN1D1UC2Ei[_ZN1D1UC2Ei]+0x3e): undefined reference to `C<int>::~C()'
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210429/45e10edc/attachment.html>


More information about the cfe-dev mailing list