Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Explicitly call destructor of template parameter's typedef

I have the following:

template <typename X> struct A {
    typedef X _X;
};

template <typename Y> struct B { // Y is struct A
    typename Y::_X x;
    void call_destructor () {
        x.~Y::_X(); // This doesn't work
        x.Y::~_X(); // This as well
    }
}; 

which doesn't compile, saying that

qualified type does not match destructor name

Using the keyword typename before the call also does not work. However, the following does compile:

template <typename Y> struct B {
    typename Y::_X x;
    typedef typename Y::_X __X;
    void call_destructor () {
        x.~__X(); // This works
    }
};

Can someone explain to me why, and is there any way to make do without the typedef?

like image 979
gmardau Avatar asked Aug 25 '17 03:08

gmardau


1 Answers

You should call the destructor differently using

x.Y::_X::~_X()

The following compile fine for me:

template <typename X> struct A {
    typedef X _X;
};

template <typename Y> struct B { // Y is struct A
    typename Y::_X x;
    void call_destructor () {
       x.Y::_X::~_X(); // This as well
    }
}; 


int main(){
  B<A<int> > b;
  b.call_destructor();
}
like image 141
Flynsee Avatar answered Oct 02 '22 13:10

Flynsee