Программы на C++. Форум разработчиков
Anonymous
Оператор по умолчанию равенства не удается в GCC для вложенного шаблонного класса, внешний класс с не дефолтом == Операт
Сообщение
Anonymous » 28 апр 2025, 15:40
Рассмотрим иерархию шаблонных классов, где тип члена не хватает явного оператора равенства. GCC отклоняет код, в то время как Кланг принимает его: < /p>
Код: Выделить всё
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
1745844050
Anonymous
Рассмотрим иерархию шаблонных классов, где тип члена не хватает явного оператора равенства. GCC отклоняет код, в то время как Кланг принимает его: < /p> [code]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; [/code] Подробнее здесь: [url]https://stackoverflow.com/questions/79596478/defaulted-equality-operator-fails-in-gcc-for-nested-templated-class-outer-class[/url]