看板 C_and_CPP 關於我們 聯絡資訊
各位大大好,最近小弟在看 C++ 17 STL ,在這之中看到了 common_type 這個函數 此函數的用途為在兩種型態中選出不會窄化(Narrowing)之形別 此函數的實做如下 template<class T1, class T2> struct common_type { using type = decltype(false ? declval<T1>() : declval<T2>()); } 小弟我不太懂此函數是如何做到篩選形別的 小弟的理解是在 ?: 運算子中將會先評估 declval<T2>() 從而得到型別 T2 (或其衍生物) 但是此函數每次都能得到應有的型別,例如 common_type<int, float> ::type; // float common_type<int, long> ::type; // long common_type<float, unsigned long>::type; // float 還請各位大大解惑 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 43.243.253.180 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1537106750.A.821.html ※ 編輯: mikukonn (43.243.253.180), 09/16/2018 22:06:46 ※ 編輯: mikukonn (43.243.253.180), 09/16/2018 22:07:31
mikukonn: 阿我看懂了,因為 ?: 運算子必須找出一個形別來回傳 09/16 22:11
Sirctal: 不錯!!! 標題改一下已解決 09/17 21:11