概念代码适用于运算符“<”但不适用于运算符“>”
The concept code works for operator "<" but doesn't work for operator ">"
因此,我们在高级编程 class 中介绍了 C++20 中的“概念”,我们在其中尝试编译以下代码。
我们尝试定义概念来限制模板对满足它们的类型的使用。
使用带有 -std=c++20.
的 gcc 10.0 可以很好地编译代码
#include <iostream>
#include <vector>
template<typename T>
concept Cute = requires(T m){
m.cuteness();
};
template<typename T>
concept Sortable = requires(T m, T n)
{
m.operator<(n);
};
template<typename T>
requires Cute<T> && Sortable<T>
void animal_sort(std::vector<T>& arr)
{
std::sort(arr.begin(), arr.end());
std::cout << "{ ";
for(const auto& elem : arr)
{
std::cout << elem << " ";
}
std::cout << "}\n";
}
class Penguin
{
private:
int _height{};
int _weight{};
int _cuteness{};
public:
Penguin() = default;
Penguin( int height, int weight, int cuteness)
: _height(height), _weight(weight), _cuteness(cuteness)
{}
int cuteness()
{
return _cuteness;
}
bool operator<(const Penguin& from)
{
return _cuteness < from._cuteness;
}
friend std::ostream& operator<<(std::ostream& os, const Penguin& penguin)
{
std::cout << "{ " << penguin._cuteness << " }";
return os;
}
};
int main()
{
std::vector<Penguin> penguins;
penguins.push_back(Penguin(10,10,5));
penguins.push_back(Penguin(7,13,1));
penguins.push_back(Penguin(9,15,3));
animal_sort<Penguin>(penguins);
}
然而,当我稍作改动并在概念代码中使用运算符“>”时:
#include <iostream>
#include <vector>
template<typename T>
concept Cute = requires(T m){
m.cuteness();
};
template<typename T>
concept Sortable = requires(T m, T n)
{
m.operator>(n);
};
template<typename T>
requires Cute<T> && Sortable<T>
void animal_sort(std::vector<T>& arr)
{
std::sort(arr.begin(), arr.end());
std::cout << "{ ";
for(const auto& elem : arr)
{
std::cout << elem << " ";
}
std::cout << "}\n";
}
class Penguin
{
private:
int _height{};
int _weight{};
int _cuteness{};
public:
Penguin() = default;
Penguin( int height, int weight, int cuteness)
: _height(height), _weight(weight), _cuteness(cuteness)
{}
int cuteness()
{
return _cuteness;
}
bool operator>(const Penguin& from)
{
return _cuteness > from._cuteness;
}
friend std::ostream& operator<<(std::ostream& os, const Penguin& penguin)
{
std::cout << "{ " << penguin._cuteness << " }";
return os;
}
};
int main()
{
std::vector<Penguin> penguins;
penguins.push_back(Penguin(10,10,5));
penguins.push_back(Penguin(7,13,1));
penguins.push_back(Penguin(9,15,3));
animal_sort<Penguin>(penguins);
}
我得到以下编译错误:
n file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_less_iter::operator()(_Iterator1, _Iterator2) const [with _Iterator1 = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Iterator2 = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1826:14: required from 'constexpr void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1866:25: required from 'constexpr void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1957:31: required from 'constexpr void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4842:18: required from 'constexpr void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]'
<source>:19:14: required from 'void animal_sort(std::vector<T>&) [with T = Penguin]'
<source>:66:25: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:45:23: error: no match for 'operator<' (operand types are 'Penguin' and 'Penguin')
45 | { return *__it1 < *__it2; }
| ~~~~~~~^~~~~~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:67,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> constexpr std::__detail::__synth3way_t<_IteratorR, _IteratorL> __gnu_cxx::operator<=>(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)' (reversed)
1112 | operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
| ^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:45:23: note: 'Penguin' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
45 | { return *__it1 < *__it2; }
| ~~~~~~~^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Val_less_iter::operator()(_Value&, _Iterator) const [with _Value = Penguin; _Iterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1806:20: required from 'constexpr void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Val_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1834:36: required from 'constexpr void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1866:25: required from 'constexpr void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1957:31: required from 'constexpr void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4842:18: required from 'constexpr void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]'
<source>:19:14: required from 'void animal_sort(std::vector<T>&) [with T = Penguin]'
<source>:66:25: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:98:22: error: no match for 'operator<' (operand types are 'Penguin' and 'Penguin')
98 | { return __val < *__it; }
| ~~~~~~^~~~~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:67,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> constexpr std::__detail::__synth3way_t<_IteratorR, _IteratorL> __gnu_cxx::operator<=>(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)' (reversed)
1112 | operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
| ^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:98:22: note: 'Penguin' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
98 | { return __val < *__it; }
| ~~~~~~^~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_less_val::operator()(_Iterator, _Value&) const [with _Iterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Value = Penguin]':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_heap.h:139:48: required from 'constexpr void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Distance = long int; _Tp = Penguin; _Compare = __gnu_cxx::__ops::_Iter_less_val]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_heap.h:246:23: required from 'constexpr void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Distance = long int; _Tp = Penguin; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_heap.h:355:22: required from 'constexpr void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1646:23: required from 'constexpr void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1917:25: required from 'constexpr void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1933:27: required from 'constexpr void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1954:25: required from 'constexpr void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4842:18: required from 'constexpr void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]'
<source>:19:14: required from 'void animal_sort(std::vector<T>&) [with T = Penguin]'
<source>:66:25: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:69:22: error: no match for 'operator<' (operand types are 'Penguin' and 'Penguin')
69 | { return *__it < __val; }
| ~~~~~~^~~~~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:67,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> constexpr std::__detail::__synth3way_t<_IteratorR, _IteratorL> __gnu_cxx::operator<=>(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)' (reversed)
1112 | operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
| ^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:69:22: note: 'Penguin' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
69 | { return *__it < __val; }
| ~~~~~~^~~~~~~
ASM generation compiler returned: 1
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_less_iter::operator()(_Iterator1, _Iterator2) const [with _Iterator1 = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Iterator2 = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1826:14: required from 'constexpr void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1866:25: required from 'constexpr void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1957:31: required from 'constexpr void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4842:18: required from 'constexpr void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]'
<source>:19:14: required from 'void animal_sort(std::vector<T>&) [with T = Penguin]'
<source>:66:25: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:45:23: error: no match for 'operator<' (operand types are 'Penguin' and 'Penguin')
45 | { return *__it1 < *__it2; }
| ~~~~~~~^~~~~~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:67,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> constexpr std::__detail::__synth3way_t<_IteratorR, _IteratorL> __gnu_cxx::operator<=>(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)' (reversed)
1112 | operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
| ^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:45:23: note: 'Penguin' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
45 | { return *__it1 < *__it2; }
| ~~~~~~~^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Val_less_iter::operator()(_Value&, _Iterator) const [with _Value = Penguin; _Iterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1806:20: required from 'constexpr void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Val_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1834:36: required from 'constexpr void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1866:25: required from 'constexpr void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1957:31: required from 'constexpr void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4842:18: required from 'constexpr void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]'
<source>:19:14: required from 'void animal_sort(std::vector<T>&) [with T = Penguin]'
<source>:66:25: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:98:22: error: no match for 'operator<' (operand types are 'Penguin' and 'Penguin')
98 | { return __val < *__it; }
| ~~~~~~^~~~~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:67,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> constexpr std::__detail::__synth3way_t<_IteratorR, _IteratorL> __gnu_cxx::operator<=>(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)' (reversed)
1112 | operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
| ^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:98:22: note: 'Penguin' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
98 | { return __val < *__it; }
| ~~~~~~^~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_less_val::operator()(_Iterator, _Value&) const [with _Iterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Value = Penguin]':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_heap.h:139:48: required from 'constexpr void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Distance = long int; _Tp = Penguin; _Compare = __gnu_cxx::__ops::_Iter_less_val]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_heap.h:246:23: required from 'constexpr void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Distance = long int; _Tp = Penguin; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_heap.h:355:22: required from 'constexpr void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1646:23: required from 'constexpr void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1917:25: required from 'constexpr void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1933:27: required from 'constexpr void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1954:25: required from 'constexpr void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4842:18: required from 'constexpr void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]'
<source>:19:14: required from 'void animal_sort(std::vector<T>&) [with T = Penguin]'
<source>:66:25: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:69:22: error: no match for 'operator<' (operand types are 'Penguin' and 'Penguin')
69 | { return *__it < __val; }
好像是什么问题?
概念代码对您的 <
-> >
更改没有任何问题。
有问题的是std::sort
。
std::sort
默认情况下假定元素将通过 <
比较排序。这不起作用,因为您的更改 <
-> >
使 class 不再有 operator<
。
std::sort
正在抱怨这个。
本质上,您的 Sortable
新概念并不是 std::sort
对元素类型所要求的 Sortable
的概念。
因此,我们在高级编程 class 中介绍了 C++20 中的“概念”,我们在其中尝试编译以下代码。 我们尝试定义概念来限制模板对满足它们的类型的使用。 使用带有 -std=c++20.
的 gcc 10.0 可以很好地编译代码#include <iostream>
#include <vector>
template<typename T>
concept Cute = requires(T m){
m.cuteness();
};
template<typename T>
concept Sortable = requires(T m, T n)
{
m.operator<(n);
};
template<typename T>
requires Cute<T> && Sortable<T>
void animal_sort(std::vector<T>& arr)
{
std::sort(arr.begin(), arr.end());
std::cout << "{ ";
for(const auto& elem : arr)
{
std::cout << elem << " ";
}
std::cout << "}\n";
}
class Penguin
{
private:
int _height{};
int _weight{};
int _cuteness{};
public:
Penguin() = default;
Penguin( int height, int weight, int cuteness)
: _height(height), _weight(weight), _cuteness(cuteness)
{}
int cuteness()
{
return _cuteness;
}
bool operator<(const Penguin& from)
{
return _cuteness < from._cuteness;
}
friend std::ostream& operator<<(std::ostream& os, const Penguin& penguin)
{
std::cout << "{ " << penguin._cuteness << " }";
return os;
}
};
int main()
{
std::vector<Penguin> penguins;
penguins.push_back(Penguin(10,10,5));
penguins.push_back(Penguin(7,13,1));
penguins.push_back(Penguin(9,15,3));
animal_sort<Penguin>(penguins);
}
然而,当我稍作改动并在概念代码中使用运算符“>”时:
#include <iostream>
#include <vector>
template<typename T>
concept Cute = requires(T m){
m.cuteness();
};
template<typename T>
concept Sortable = requires(T m, T n)
{
m.operator>(n);
};
template<typename T>
requires Cute<T> && Sortable<T>
void animal_sort(std::vector<T>& arr)
{
std::sort(arr.begin(), arr.end());
std::cout << "{ ";
for(const auto& elem : arr)
{
std::cout << elem << " ";
}
std::cout << "}\n";
}
class Penguin
{
private:
int _height{};
int _weight{};
int _cuteness{};
public:
Penguin() = default;
Penguin( int height, int weight, int cuteness)
: _height(height), _weight(weight), _cuteness(cuteness)
{}
int cuteness()
{
return _cuteness;
}
bool operator>(const Penguin& from)
{
return _cuteness > from._cuteness;
}
friend std::ostream& operator<<(std::ostream& os, const Penguin& penguin)
{
std::cout << "{ " << penguin._cuteness << " }";
return os;
}
};
int main()
{
std::vector<Penguin> penguins;
penguins.push_back(Penguin(10,10,5));
penguins.push_back(Penguin(7,13,1));
penguins.push_back(Penguin(9,15,3));
animal_sort<Penguin>(penguins);
}
我得到以下编译错误:
n file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_less_iter::operator()(_Iterator1, _Iterator2) const [with _Iterator1 = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Iterator2 = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1826:14: required from 'constexpr void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1866:25: required from 'constexpr void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1957:31: required from 'constexpr void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4842:18: required from 'constexpr void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]'
<source>:19:14: required from 'void animal_sort(std::vector<T>&) [with T = Penguin]'
<source>:66:25: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:45:23: error: no match for 'operator<' (operand types are 'Penguin' and 'Penguin')
45 | { return *__it1 < *__it2; }
| ~~~~~~~^~~~~~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:67,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> constexpr std::__detail::__synth3way_t<_IteratorR, _IteratorL> __gnu_cxx::operator<=>(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)' (reversed)
1112 | operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
| ^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:45:23: note: 'Penguin' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
45 | { return *__it1 < *__it2; }
| ~~~~~~~^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Val_less_iter::operator()(_Value&, _Iterator) const [with _Value = Penguin; _Iterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1806:20: required from 'constexpr void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Val_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1834:36: required from 'constexpr void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1866:25: required from 'constexpr void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1957:31: required from 'constexpr void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4842:18: required from 'constexpr void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]'
<source>:19:14: required from 'void animal_sort(std::vector<T>&) [with T = Penguin]'
<source>:66:25: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:98:22: error: no match for 'operator<' (operand types are 'Penguin' and 'Penguin')
98 | { return __val < *__it; }
| ~~~~~~^~~~~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:67,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> constexpr std::__detail::__synth3way_t<_IteratorR, _IteratorL> __gnu_cxx::operator<=>(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)' (reversed)
1112 | operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
| ^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:98:22: note: 'Penguin' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
98 | { return __val < *__it; }
| ~~~~~~^~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_less_val::operator()(_Iterator, _Value&) const [with _Iterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Value = Penguin]':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_heap.h:139:48: required from 'constexpr void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Distance = long int; _Tp = Penguin; _Compare = __gnu_cxx::__ops::_Iter_less_val]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_heap.h:246:23: required from 'constexpr void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Distance = long int; _Tp = Penguin; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_heap.h:355:22: required from 'constexpr void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1646:23: required from 'constexpr void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1917:25: required from 'constexpr void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1933:27: required from 'constexpr void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1954:25: required from 'constexpr void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4842:18: required from 'constexpr void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]'
<source>:19:14: required from 'void animal_sort(std::vector<T>&) [with T = Penguin]'
<source>:66:25: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:69:22: error: no match for 'operator<' (operand types are 'Penguin' and 'Penguin')
69 | { return *__it < __val; }
| ~~~~~~^~~~~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:67,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> constexpr std::__detail::__synth3way_t<_IteratorR, _IteratorL> __gnu_cxx::operator<=>(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)' (reversed)
1112 | operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
| ^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:69:22: note: 'Penguin' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
69 | { return *__it < __val; }
| ~~~~~~^~~~~~~
ASM generation compiler returned: 1
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_less_iter::operator()(_Iterator1, _Iterator2) const [with _Iterator1 = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Iterator2 = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1826:14: required from 'constexpr void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1866:25: required from 'constexpr void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1957:31: required from 'constexpr void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4842:18: required from 'constexpr void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]'
<source>:19:14: required from 'void animal_sort(std::vector<T>&) [with T = Penguin]'
<source>:66:25: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:45:23: error: no match for 'operator<' (operand types are 'Penguin' and 'Penguin')
45 | { return *__it1 < *__it2; }
| ~~~~~~~^~~~~~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:67,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> constexpr std::__detail::__synth3way_t<_IteratorR, _IteratorL> __gnu_cxx::operator<=>(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)' (reversed)
1112 | operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
| ^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:45:23: note: 'Penguin' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
45 | { return *__it1 < *__it2; }
| ~~~~~~~^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Val_less_iter::operator()(_Value&, _Iterator) const [with _Value = Penguin; _Iterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1806:20: required from 'constexpr void std::__unguarded_linear_insert(_RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Val_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1834:36: required from 'constexpr void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1866:25: required from 'constexpr void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1957:31: required from 'constexpr void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4842:18: required from 'constexpr void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]'
<source>:19:14: required from 'void animal_sort(std::vector<T>&) [with T = Penguin]'
<source>:66:25: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:98:22: error: no match for 'operator<' (operand types are 'Penguin' and 'Penguin')
98 | { return __val < *__it; }
| ~~~~~~^~~~~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:67,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> constexpr std::__detail::__synth3way_t<_IteratorR, _IteratorL> __gnu_cxx::operator<=>(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)' (reversed)
1112 | operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs,
| ^~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_iterator.h:1112:5: note: template argument deduction/substitution failed:
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:71,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/char_traits.h:39,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ios:40,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/ostream:38,
from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/iostream:39,
from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:98:22: note: 'Penguin' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
98 | { return __val < *__it; }
| ~~~~~~^~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h: In instantiation of 'constexpr bool __gnu_cxx::__ops::_Iter_less_val::operator()(_Iterator, _Value&) const [with _Iterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Value = Penguin]':
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_heap.h:139:48: required from 'constexpr void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Distance = long int; _Tp = Penguin; _Compare = __gnu_cxx::__ops::_Iter_less_val]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_heap.h:246:23: required from 'constexpr void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Distance = long int; _Tp = Penguin; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_heap.h:355:22: required from 'constexpr void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1646:23: required from 'constexpr void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1917:25: required from 'constexpr void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1933:27: required from 'constexpr void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:1954:25: required from 'constexpr void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algo.h:4842:18: required from 'constexpr void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<Penguin*, std::vector<Penguin> >]'
<source>:19:14: required from 'void animal_sort(std::vector<T>&) [with T = Penguin]'
<source>:66:25: required from here
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/predefined_ops.h:69:22: error: no match for 'operator<' (operand types are 'Penguin' and 'Penguin')
69 | { return *__it < __val; }
好像是什么问题?
概念代码对您的 <
-> >
更改没有任何问题。
有问题的是std::sort
。
std::sort
默认情况下假定元素将通过 <
比较排序。这不起作用,因为您的更改 <
-> >
使 class 不再有 operator<
。
std::sort
正在抱怨这个。
本质上,您的 Sortable
新概念并不是 std::sort
对元素类型所要求的 Sortable
的概念。