[Bug c++/101872] static_cast succeeds in CRTP with incorrect type
pinskia at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Aug 12 15:17:48 GMT 2021
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101872
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Original testcase (since we won't know if godbolt will be around in 10-20 years
...):
#include <tuple>
template <template <typename> typename ContainerImpl, typename Policy>
struct BaseContainer {
using Impl = ContainerImpl<Policy>; // this isn't our actual Impl (missing
Handlers template args...)...
Impl & impl() { return static_cast<Impl&>(*this); } // this shouldn't
compile
int start() {
return impl().on_start(); // ... so impl() casts this to incorrect
value
}
};
template <typename Policy, typename ...Handlers>
struct BaseUiContainer : BaseContainer<BaseUiContainer, Policy> {
int on_start() {
return m_x * 2; // ... and because of the wrong cast this->m_x is
pointing elsewhere
}
private:
std::tuple<Handlers...> m_handlers; // comment this out to get the correct
output of 4
int m_x = 2;
};
struct Handler {
char a[128];
};
template <typename Policy>
struct MyUiContainer : BaseUiContainer<Policy, Handler> {
};
int main() {
struct P {};
MyUiContainer<P> container;
return container.start();
}
More information about the Gcc-bugs
mailing list