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.

Reply via email to