mauve
mauve

Reputation: 2016

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.

Upvotes: 1

Views: 174

Answers (1)

Jarod42
Jarod42

Reputation: 217810

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

Upvotes: 1

Related Questions