Andrey Tarasevich
2024-09-20 02:59:23 UTC
Seems to be reproducible in all Clang versions
struct A {};
template <typename = int> struct C
{
C() = default;
C(const A &) {}
C &operator =(const A &a) { return operator =(C(a)); }
};
int main()
{
A a;
C<> c;
c = a;
}
Results in
error: no viable conversion from 'C<>' to 'const A'
7 | C &operator =(const A &a) { return operator =(C(a)); }
| ^~~~
It appears that a user-declared converting assignment operator somehow
suppressed the compiler-provided copy-assignment operator for `C<>`. The
issue is only present when `C` is a template.
The problem can be fixed by calling the compiler-provided
copy-assignment operator through an explicit `this->`
C &operator =(const A &a) { return this->operator =(C(a)); }
or by adding an explicit declaration of the copy-assignment operator
C &operator =(const C &) = default;
Am I missing something obvious here? GCC and MSVC++ accept the original
version of the code without any issues.
struct A {};
template <typename = int> struct C
{
C() = default;
C(const A &) {}
C &operator =(const A &a) { return operator =(C(a)); }
};
int main()
{
A a;
C<> c;
c = a;
}
Results in
error: no viable conversion from 'C<>' to 'const A'
7 | C &operator =(const A &a) { return operator =(C(a)); }
| ^~~~
It appears that a user-declared converting assignment operator somehow
suppressed the compiler-provided copy-assignment operator for `C<>`. The
issue is only present when `C` is a template.
The problem can be fixed by calling the compiler-provided
copy-assignment operator through an explicit `this->`
C &operator =(const A &a) { return this->operator =(C(a)); }
or by adding an explicit declaration of the copy-assignment operator
C &operator =(const C &) = default;
Am I missing something obvious here? GCC and MSVC++ accept the original
version of the code without any issues.
--
Best regards,
Andrey
Best regards,
Andrey