今日老友問我一個 c艹 的報錯問題,以下兩個版本,一個對,一個錯:
能夠編譯經過:ios
#include <algorithm> #include <iostream> using namespace std; struct Stu { int age; bool operator<(const Stu& s) { return age > s.age; } }; int main() { Stu arr[2]; arr[0].age = 6; arr[1].age = 8; sort(arr, arr + 2); // [arr,arr+2) for (auto& item : arr) { cout << item.age << ' ' ; } return 0; }
如上正常使用,沒有報錯。
咱們知道 sort 缺省函數爲 less,以下模板類:app
template<> struct less<void> { // transparent functor for operator< typedef int is_transparent; template<class _Ty1, class _Ty2> constexpr auto operator()(_Ty1&& _Left, _Ty2&& _Right) const -> decltype(static_cast<_Ty1&&>(_Left) < static_cast<_Ty2&&>(_Right)) { // transparently apply operator< to operands return (static_cast<_Ty1&&>(_Left) < static_cast<_Ty2&&>(_Right)); } };
重點看這句話,參數類型非 const。less
auto operator()(_Ty1&& _Left, _Ty2&& _Right)
而後咱們再看下文報錯的狀況函數
可是若是老友使用 set 函數:
error C2678: 二進制「<」: 沒有找到接受「const _Ty」類型的左操做數的運算符(或沒有可接受的轉換)this
#include <set> #include <algorithm> #include <iostream> using namespace std; struct Stu { int age; bool operator<(const Stu& s) { return age > s.age; } }; int main() { set<Stu> my_set; my_set.insert(Stu{ 6 }); my_set.insert(Stu{ 8 }); for (auto& item : my_set) { cout << item.age << ' ' ; } return 0; }
修改如下,能夠編譯經過了:spa
bool operator<(const Stu& s) const // 添加 const
老友問我爲何?
咱們能夠經過觀察報錯說明來看:
有一句話:編譯 類 模板 成員函數 "bool std::less<_Kty>::operator ()(const _Ty &,const _Ty &) const" 時。。。
由於咱們自定義了 Stu 類的排序規則,即重載了 operator<..net
Set 內部排序缺省函數位 less
咱們去看一下類模板 less指針
template<class _Ty = void> struct less { // functor for operator< constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const { // apply operator< to operands return (_Left < _Right); } };
看到參數是 const _Ty& _Left, const _Ty& _Right,因此傳入的 this 對象引用變成 const _Ty& _Left,而 _Left 是 const 致使沒法調用非 cosnt 成員函數 opertator<。因此須要添加 const 使其變成 const 成員函數code
關於const的做用:
表示成員函數隱含傳入的this指針爲const指針
後面加 const表示函數不能夠修改class的成員
https://blog.csdn.net/SMF0504/article/details/52311207對象