std::is_function fail!

Why this fails:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <type_traits>

template <typename T>
void foo(T t)
{
	static_assert(  std::is_function<decltype(t)>::value, "Not a function!" );
	t();
}

void bar()
{
	std::cout << "This is bar!" << std::endl;
}

int main()
{
	foo(bar);

	std::cin.ignore();
	return 0;
}


bar is function, right?

Thanks for your time.
bar is a function.

On invocation of foo(bar), T is deduced to be a pointer to function; and t is initialized with pointer to bar
Thanks.
Would this work for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <type_traits>

template<typename Type>
struct is_function_pointer
{
    static const bool 
    value = std::is_pointer<Type>::value ?
            std::is_function<typename std::remove_pointer<Type>::type>::value :
            false;
};


template <typename T>
void foo(T t)
{
	static_assert(  is_function_pointer<T>::value , "Not a function!" );
	t();
}

void bar()
{
	std::cout << "This is bar!" << std::endl;
}

int main()
{
	foo(bar);

	std::cin.ignore();
	return 0;
}
Topic archived. No new replies allowed.