public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101107] New: Misleading error message in aggregate initialization in CRTP base class
@ 2021-06-17 13:02 btzy1996 at gmail dot com
  2021-06-22 20:53 ` [Bug c++/101107] " rs2740 at gmail dot com
  2021-06-27 14:39 ` btzy1996 at gmail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: btzy1996 at gmail dot com @ 2021-06-17 13:02 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101107
           Summary: Misleading error message in aggregate initialization
                    in CRTP base class
           Product: gcc
           Version: 11.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: btzy1996 at gmail dot com
  Target Milestone: ---

The following code gives a compile error:

```
template <typename Prev, typename Next>
struct Combiner;

template <typename T>
struct Base {
    template <typename Next>
    auto dostuff(Next next) {
        return Combiner<T, Next>{Base<Combiner<T, Next>>{}, *this, next};
    }
};

template <typename Prev, typename Next>
struct Combiner : public Base<Combiner<Prev, Next>> {
    Prev prev;
    Next next;
};

struct A : public Base<A> {
    int stuff;
};

struct B : public Base<B> {
    int stuff;
};

int main(){
    A{{}, 1}.dostuff(B{{}, 2});
}
```

Error message:
```
<source>: In instantiation of 'auto Base<T>::dostuff(Next) [with Next = B; T =
A]':
<source>:27:21:   required from here
<source>:8:68: error: cannot convert 'B' to 'int' in initialization
    8 |         return Combiner<T, Next>{Base<Combiner<T, Next>>{}, *this,
next};
      |                                                                    ^~~~
      |                                                                    |
      |                                                                    B
Compiler returned: 1
```

However, the error message is misleading - it seems clear that the programmer
forgot to cast `*this` to the derived type (i.e. change `*this` to
`static_cast<const T&>(*this)`), but the error message is about something
totally different.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/101107] Misleading error message in aggregate initialization in CRTP base class
  2021-06-17 13:02 [Bug c++/101107] New: Misleading error message in aggregate initialization in CRTP base class btzy1996 at gmail dot com
@ 2021-06-22 20:53 ` rs2740 at gmail dot com
  2021-06-27 14:39 ` btzy1996 at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: rs2740 at gmail dot com @ 2021-06-22 20:53 UTC (permalink / raw)
  To: gcc-bugs

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

TC <rs2740 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rs2740 at gmail dot com

--- Comment #1 from TC <rs2740 at gmail dot com> ---
Since `*this` can't initialize an `A`, it's assuming brace elision. Not sure
how the diagnostic can be made clearer though...perhaps it can report that
assumption?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/101107] Misleading error message in aggregate initialization in CRTP base class
  2021-06-17 13:02 [Bug c++/101107] New: Misleading error message in aggregate initialization in CRTP base class btzy1996 at gmail dot com
  2021-06-22 20:53 ` [Bug c++/101107] " rs2740 at gmail dot com
@ 2021-06-27 14:39 ` btzy1996 at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: btzy1996 at gmail dot com @ 2021-06-27 14:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Bernard Teo <btzy1996 at gmail dot com> ---
Hmm, I didn't realise it was brace elision - the error makes much more sense
this way.  Reporting the assumption (e.g. "`A` can't be initialized from
`*this`, assuming brace elision") would solve the problem.  In my opinion the
important part of the message is that "`A` can't be initialized from `*this`" -
just that part should point the programmer in the right direction.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-06-27 14:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-17 13:02 [Bug c++/101107] New: Misleading error message in aggregate initialization in CRTP base class btzy1996 at gmail dot com
2021-06-22 20:53 ` [Bug c++/101107] " rs2740 at gmail dot com
2021-06-27 14:39 ` btzy1996 at gmail dot com

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).