#include <iostream> #include <string> #include <vector> struct Customer{ Customer(std::string name_):name(name_){} std::string name; }; struct CustomerEq{ bool operator() (const Customer& l, const Customer& r) { return l.name == r.name; } }; struct CustomerHash{ size_t operator()(const Customer& c) { return std::hash<std::string>()(c.name); } }; // 使用变成实现多重继承 template<typename... Bases> struct CustomerOp: Bases... { // 使用父类的仿函数对象 using Bases::operator()...; }; void Test(){ CustomerOp<CustomerHash, CustomerEq> cust; Customer c("ts"); std::cout<<cust(c)<<std::endl; std::cout<<std::boolalpha<<cust(c, Customer("js"))<<std::endl; } int main() { Test(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter