Explicit destructor call with pattern aliases and inheritance
Inspired by the question and its answer Calling the destructor with decltype and \ or std :: remove_reference , I tried to do the same in my code. Sorry, next MWE
template<class T>
using alias = T;
class Yo {
public:
~Yo() { }
};
template<class A>
class Lo {
protected:
A a;
};
template<class A>
class Foobar : public Lo<A> {
public:
typedef Lo<A> super;
inline void expl() {
super::a.~alias<decltype(super::a)>();
}
};
int main() {
Foobar<Yo> A;
A.expl();
}
compiles with g ++ but with clang I get
mwe.cpp: 20: 27: error: "alias" after the keyword "template" does not reference the template
And with icpc I get
mwe.cpp (20): internal error: bad pointer
I'm not sure if this is correct C ++, or if this is a bug inside the compilers. Do you see a workaround?
source to share
I found a way to make it work. But for this I had to make the variable a in the public class
template<class T>
using alias = T;
class Yo {
public:
~Yo() { }
};
template<class A>
class Lo {
public:
A a;
};
template<class A>
class Foobar : public Lo<A> {
public:
typedef Lo<A> super;
using type = alias<decltype(super::a)>;
inline void expl() {
super::a.~type();
}
};
int main() {
Foobar<Yo> A;
A.expl();
}
EDIT I ββjust thought of something
In the Foobar class, you will get a Parameters A template that will be passed to the Lo superclass. So the variable a is of the type that you get in your template! Therefore, you can simply:
using type = alias<A>;
inline void expl() {
super::a.~type();
}
You can now have a protected variable reference
source to share