public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "wuyongwei at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/98283] New: decltype(auto) may be deduce static data member to wrong type
Date: Tue, 15 Dec 2020 01:13:57 +0000	[thread overview]
Message-ID: <bug-98283-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 98283
           Summary: decltype(auto) may be deduce static data member to
                    wrong type
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wuyongwei at gmail dot com
  Target Milestone: ---

The following code shows that `decltype(auto)` is deduced to the wrong type
when used in template code for an expression like `(var.static_data_member>)`.

```cpp
#include <cstddef>
#include <type_traits>
#include <utility>

template <typename T>
struct type_displayer;

#define TYPE_DISPLAY(x) type_displayer<decltype(x)> test_obj;

struct SkipField {};

struct TestSkip {
    template <typename T, std::size_t> struct FIELD;

    int value;
    static SkipField unused;

    template <typename T>
    struct FIELD<T, 0> {
        T &&obj;
        FIELD(T &&ref) : obj(std::forward<T>(ref))
        {
        }
        decltype(auto) value()
        {
            return (obj.value);
        }
        static constexpr const char *name()
        {
            return "value";
        }
    };

    template <typename T>
    struct FIELD<T, 0 + 1> {
        T &&obj;
        FIELD(T &&ref) : obj(std::forward<T>(ref))
        {
        }
        decltype(auto) value()
        {
            return (obj.unused);
        }
        decltype((obj.unused)) valueAlt()
        {
            return (obj.unused);
        }
        static constexpr const char *name()
        {
            return "unused";
        }
    };
};

int main()
{
    TestSkip s;
    // decltype((TestSkip::FIELD<TestSkip&, 0>(s).value()))    is int&
    // decltype((TestSkip::FIELD<TestSkip&, 1>(s).value()))    is SkipField,
but it should be SkipField&
    // decltype((TestSkip::FIELD<TestSkip&, 1>(s).valueAlt())) is SkipField&
}
```

This is a simplified usage scenario, where I want to use a macro to generate
code to emulate static reflection. Here is the problematic part:

```cpp
        decltype(auto) value()
        {
            return (obj.unused);
        }
```

This function returns `SkipField`, instead of the expected `SkipField&`. I have
verified that clang does what I want.

Currently I have to avoid `decltype(auto)` as a workaround:

```cpp
        decltype((obj.unused)) valueAlt()
        {
            return (obj.unused);
        }
```

(I used the macro TYPE_DISPLAY to check the type of an expression.)

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

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-15  1:13 wuyongwei at gmail dot com [this message]
2021-08-03  5:33 ` [Bug c++/98283] decltype(auto) may deduce a static data member to wrong type in a template pinskia at gcc dot gnu.org
2023-05-07 15:58 ` cvs-commit at gcc dot gnu.org
2023-05-07 15:58 ` ppalka 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-98283-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).