public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "pinskia at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/101872] static_cast succeeds in CRTP with incorrect type
Date: Thu, 12 Aug 2021 15:17:48 +0000	[thread overview]
Message-ID: <bug-101872-4-Sq9g25hkk7@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-101872-4@http.gcc.gnu.org/bugzilla/>

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();
}

      reply	other threads:[~2021-08-12 15:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-12  8:11 [Bug c++/101872] New: " vpervouchine at gmail dot com
2021-08-12 15:17 ` pinskia at gcc dot gnu.org [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-101872-4-Sq9g25hkk7@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).