// // Created by light on 19-11-3. // #include #include #include using namespace std; // 1.used to declare return tyoes è¿åç±»å // c++2.0ä¹åç¼è¯ä¸åºæ¥ // template // decltype(x + y) add(T1 x, T2 y); // ç¼è¯å¨ç¼ä¸åºæ¥å¨2.0ä¹ååæä¸é¢è¿ä¸ª,å ç¨autoæå®,åé¢è¿åç±»å为decltype(x+y) template auto add(T1 x, T2 y) -> decltype(x + y) { // -> ä¹ç¨å¨lambda return x + y; } class Person { public: string firstname; string lastname; }; int main() { // 1.used to declare return tyoes cout << add(1, 2) << endl; // 2.模æ¿å ç¼ç¨ ä¾å¦å¨ä¸ä¸ªæ¨¡æ¿å½æ°æç±»è·å容å¨çvalue_type,è¿éå°±ä¸å°è£ äº,ç´æ¥åå¨main彿°éé¢ // è·å¾è¡¨è¾¾å¼çtype æç¹åtypeof()ç¹ç¹ map coll; // è·åä¸è¿°ç±»å decltype(coll)::value_type m{"as", 1}; // value_type为pair m cout << m.first << " " << m.second << endl; pair p{"a", 2}; cout << p.first << " " << p.second << endl; // 3.used to pass the type of a lambda // æ¯å¤§å° auto cmp = [](const Person &p1, const Person &p2) { return p1.lastname < p2.lastname; }; // 对äºlambda,æä»¬å¾å¾åªæobject,å¾å°æäººè½å¤ååºå®çç±»åï¼èææ¶å°±éè¦ç¥éå®çç±»å,è¦è·å¾å ¶type,å°±è¦åå©å ¶decltype set col(cmp); return 0; }