[英]Why does std::is_function<F> return false_type when F is deduced?
[英]Why does std::is_function evaluate to false when using on a dereferenced function pointer?
我試圖使用std::is_function
來確定變量是否是函數指針。
運行以下代碼時
#include <iostream>
#include <typeinfo>
using namespace std;
int main() {
typedef int(*functionpointer)();
functionpointer pmain = main;
cout << typeid(functionpointer).name() << " "<< is_function<functionpointer>::value << endl;
cout << typeid(decltype(pmain)).name() << " " << is_function<decltype(pmain)>::value << endl;
cout << typeid(decltype(main)).name() << " " << is_function<decltype(main)>::value << endl;
cout << typeid(decltype(*pmain)).name() << " " << is_function<decltype(*pmain)>::value << endl;
return 0;
}
輸出是
PFivE 0
PFivE 0
FivE 1
FivE 0
任何有洞察力的人std::is_function
解釋為什么std::is_function
的最后一個表達式求值為false嗎?
(在g ++ 4.7,g ++ 4.8和clang ++ 3.2下測試的代碼)
這是因為decltype(*pmain)
產生對函數類型的引用 ,對於該函數類型, std::function
為false
。 嘗試:
is_function<remove_reference<decltype(*pmain)>::type>::value
BTW:ISO C ++禁止獲取:: main()的地址
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.