This is probably something new in the standard. The following program won't compile in the Clang 32-bit mode, but compiles and links using the classic compiler:
#pragma hdrstop
#pragma argsused
#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif
#include <stdio.h>
#include <complex>
#include <vector>
#include <algorithm>
typedef std::complex<double> Complex;
bool operator<(const Complex & lhs, const Complex & rhs)
{
return lhs.real() < rhs.real();
}
void doSort()
{
std::vector<Complex> vc;
std::sort(vc.begin(), vc.end());
}
int _tmain(int argc, _TCHAR* argv[])
{
doSort();
return 0;
}
The errors are:
[bcc32c Error] algorithm(3077): no matching function for call to object of type 'std::less<void>'
algorithm(3094): in instantiation of function template specialization 'std::_Med3<std::complex<double> *, std::less<void> >' requested here
algorithm(3109): in instantiation of function template specialization 'std::_Median<std::complex<double> *, std::less<void> >' requested here
algorithm(3171): in instantiation of function template specialization 'std::_Unguarded_partition<std::complex<double> *, std::less<void> >' requested here
algorithm(3201): in instantiation of function template specialization 'std::_Sort<std::complex<double> *, int, std::less<void> >' requested here
algorithm(3208): in instantiation of function template specialization 'std::sort<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::complex<double> > > >, std::less<void> >' requested here
File4.cpp(26): in instantiation of function template specialization 'std::sort<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::complex<double> > > > >' requested here
xstddef(339): candidate template ignored: substitution failure [with _Ty1 = std::complex<double> &, _Ty2 = std::complex<double> &]: invalid operands to binary expression ('std::complex<double>' and 'std::complex<double>')
We get the same errors for any algorithm that relies on std::less, like lower_bound, min_element, and sets. Is this behavior per C++11 or C++14? What do we have to do to get std::less to use the operator< for the Complex type?
Thanks.
Connect with Us