public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "matthieum.147192 at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/98288] New: Accidental equality of classes templated by pointer to local static constant of templated function
Date: Tue, 15 Dec 2020 13:58:54 +0000	[thread overview]
Message-ID: <bug-98288-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98288

            Bug ID: 98288
           Summary: Accidental equality of classes templated by pointer to
                    local static constant of templated function
           Product: gcc
           Version: 7.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: matthieum.147192 at gmail dot com
  Target Milestone: ---

Based on https://stackoverflow.com/q/65306562/147192.

The behavior of GCC (and Clang) is inconsistent (between compile-time and
run-time), however it is unclear to me whether the inconsistency is conforming
with the C++17 standard or not.


The following reduced program is expected to return 0 (invoking g++ with
-std=c++17):

template <typename, typename>
struct is_same { static constexpr bool value = false; };

template <typename T>
struct is_same<T, T> { static constexpr bool value = true; };

template <typename T, typename U>
static constexpr bool is_same_v = is_same<T, U>::value;

using uintptr_t = unsigned long long;

template <int const* I>
struct Parameterized { int const* member; };

template <typename T>
auto create() {
    static constexpr int const I = 2;

    return Parameterized<&I>{ &I };
}

int main() {
    auto one = create<short>();
    auto two = create<int>();

    if (is_same_v<decltype(one), decltype(two)>) {
        return reinterpret_cast<uintptr_t>(one.member) ==
reinterpret_cast<uintptr_t>(two.member) ? 1 : 2;
    }

    return 0;
}

Yet, on all versions of GCC where it compiles (from 7.2 onwards), and for all
optimization levels (from -O0 to -O3), it returns 2, indicating:

- That `one` and `two` have the same type -- which according to 17.4
[temp.type] should mean that they point to the same object.
- Yet they point to different objects -- there are two instances of
`create<T>()::I`, one for `T = short` and one for `T = int`.

The assembly listing clearly contains 2 different instances of
`create<T>()::I`.


Notes:

- If `I` is initialized with `= sizeof(T)`, instead, then the program returns 0
as expected.
- Clang (up to 11.0) fails to compile the program with -O0 (the linker failing
to find a `create()::I` variable), and otherwise produces the same result as
GCC with -O1 to -O3.

             reply	other threads:[~2020-12-15 13:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-15 13:58 matthieum.147192 at gmail dot com [this message]
2020-12-15 14:38 ` [Bug c++/98288] " matthieum.147192 at gmail dot com
2021-01-04 11:57 ` matthieum.147192 at gmail dot com
2021-02-02  9:44 ` matthieum.147192 at gmail dot com
2021-10-02  6:10 ` pinskia at gcc dot gnu.org

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-98288-4@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).