Код: Выделить всё
template
struct Wrapper {
T x;
constexpr bool operator==(const Wrapper& other) const {
return x == other.x; // Depends on T's operator==
}
};
template
struct Pair {
T first;
T second;
constexpr bool operator==(const Pair&) const = default; // Uses Wrapper's operator==
};
struct A { int x; }; // No explicit operator== defined
int main() {
[[maybe_unused]]Pair p{{4}, {2}}; // GCC fails here during Pair instantiation
}
< /code>
demo < /p>
поведение компилятора: < /strong> < /p>
[*] Clang 20.1.0 (C ++ 23): компилированные успешные < /li>
gcc 14.2 (c ++ 23): провалится с
gcc 14.2 (c ++): сбой с
gcc 14.2 (c ++): In instantiation of 'constexpr bool Wrapper::operator==(const Wrapper&) const [with T = A]':
:15:20: required from 'struct Pair'
15 | constexpr bool operator==(const Pair&) const = default;
| ^~~~~~~~
:23:22: required from here
23 | Pair p{{4}};
| ^
:5:18: error: no match for 'operator==' (operand types are 'const A' and 'const A')
5 | return x == other.x;
| ~~^~~~~~~~~~
Compiler returned: 1
< /code>
< /li>
< /ul>
Ключевой вопрос: < /strong>
это ошибка GCC, или Clang чрезвычайно допустим? /> // Before (fails in GCC)
constexpr bool operator==(const Pair&) const = default;
// After (works)
bool operator==(const Pair&) const = default;
< /code>
< /li>
вручную реализовать Pare :: operator ==
constexpr bool operator==(const Pair& other) const {
return first == other.first && second == other.second;
}
< /code>
< /li>
По умолчанию warper :: operator == вместо пользовательской реализации
// Before (triggers GCC bug)
constexpr bool operator==(const Wrapper& other) const {
return x == other.x;
}
// After (works)
constexpr bool operator==(const Wrapper&) const = default;
Подробнее здесь: https://stackoverflow.com/questions/795 ... uter-class