[英]Why does std::is_function evaluate to false when using on a dereferenced function pointer?
[英]Why does std::is_function<F> return false_type when F is deduced?
給定以下代碼,自動推斷出類型為Function
代碼,當我斷言Function
是否為使用std::is_function<Function>
的函數時,會得到意外的結果:
#include <iostream>
#include <iomanip>
#include <type_traits>
template <typename Function>
bool test_type(Function&& f)
{
return std::is_function<Function>::value;
}
template <typename Function>
bool test_decltype(Function&& f)
{
return std::is_function<decltype(f)>::value;
}
int f()
{
return 1;
}
int main()
{
std::cout << std::boolalpha
<< "is_function<Function>: " << test_type(f) << std::endl
<< "is_function<decltype(f)>: " << test_decltype(f) << std::endl
<< std::endl
<< "Explicit type:" << std::endl
<< "is_function<Function>: " << test_type<int()>(f) << std::endl
<< "is_function<decltype(f)>: " << test_decltype<int()>(f) << std::endl;
return 0;
}
但是,結果是(此處: http ://ideone.com/Jy1sFA,已使用MSVC2013.4在本地驗證):
is_function<Function>: false
is_function<decltype(f)>: false
Explicit type:
is_function<Function>: true
is_function<decltype(f)>: false
我期望is_function<Function>
即使在推論的情況下也為true_type
。 老實說,我什至都希望is_function<decltype(f)>
在這兩種情況下都是true_type
,但事實並非如此。
您還可以使用std::remove_reference
來獲取類型的額外參考:
template <typename Function>
bool test_type(Function&& f)
{
return std::is_function<typename std::remove_reference<Function>::type>::value;
}
template <typename Function>
bool test_decltype(Function&& f)
{
return std::is_function<typename std::remove_reference<decltype(f)>::type>::value;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.