I wrote a simple mymax which is a wrapper over std::max, but it fails to work with error primary-expression before decltype, The code is:
#include <functional>
#include <iostream>
using namespace std;
class car
{
public:
int cost;
friend ostream& operator<<(ostream& os, car c);
};
ostream& operator<<(ostream& os, car c) {
os << c.cost;
return os;
}
template <typename T>
T mymax(T a, T b, function<T (T a, T b)> comp = less<T>())
{
return std::max(a,b, comp);
}
int main()
{
car bmw, audi;
bmw.cost = 10;
audi.cost = 9;
auto carcomaprator = [](const car& l, const car& r) -> bool { return l.cost < r.cost;};
cout<< mymax<car>(bmw, audi, decltype(carcomaprator)) << endl; // <------- fails here
cout<< mymax<int>(8, 9) << endl;
return 0;
}
why I am not able to pass car comparator and how to fix this and use lambda itself. This is only for learning purpose. I am not able to understand why decltype is not able to in this case.
Aucun commentaire:
Enregistrer un commentaire