[問題] C++ STL function common_type 實作行為(已解決)

看板C_and_CPP作者 (mikukon)時間5年前 (2018/09/16 22:05), 5年前編輯推噓1(101)
留言2則, 2人參與, 5年前最新討論串1/1
各位大大好,最近小弟在看 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

09/16 22:11, 5年前 , 1F
阿我看懂了,因為 ?: 運算子必須找出一個形別來回傳
09/16 22:11, 1F

09/17 21:11, 5年前 , 2F
不錯!!! 標題改一下已解決
09/17 21:11, 2F
文章代碼(AID): #1RdcC-WX (C_and_CPP)