以下のコード例では、オーバーロードされた operator< は const 修飾されておらず、Visual C++ (2013 Preview までのすべてのバージョン) でコンパイルされますが、Clang ではエラーがスローされます - 注: 候補関数は実行可能ではありません: 'this' 引数タイプは「const Entry」ですが、メソッドは const bool operator<( const Entry& other ) とマークされていません。
#include "stdafx.h"
#include <vector>
#include <algorithm>
struct Entry
{
unsigned int age;
bool operator<( const Entry& other ) // !!! no const qualification here !!!
{
return age < other.age;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<Entry> entries;
for( unsigned int i = 0; i < 100; ++i )
{
Entry entry;
entry.age = i;
entries.push_back( entry );
}
// Sort by age
std::sort( entries.begin(), entries.end() );
return 0;
}
Visual C++ は、比較/関係演算子の const の正確性を強制する際に標準に準拠していませんか? または、これは std::sort と関係がありますか?