简体   繁体   中英

Why does std::is_function<F> return false_type when F is deduced?

Given the following code, where the type Function is automatically deduced, I get unexpected results when I assert whether Function is a function using 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;
}

The results are however (Here: http://ideone.com/Jy1sFA , verified locally using MSVC2013.4):

is_function<Function>:    false
is_function<decltype(f)>: false

Explicit type:
is_function<Function>:    true
is_function<decltype(f)>: false

I expected is_function<Function> to be true_type even in the deduced case. To be honest I even expected is_function<decltype(f)> to be true_type in both cases but alas it is not.

You have extra reference for your type you may use 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;
}

Live example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM