#include <functional>
int foo(void) {return 2;}
class bar {
public:
int operator() (void) {return 3;};
int something(int a) {return a;};
};
template <class C> auto func(C&& c) -> decltype(c()) { return c(); }
template <class C> int doit(C&& c) { return c();}
template <class C> void func_wrapper(C&& c) { func( std::bind(doit<C>, std::forward<C>(c)) ); }
int main(int argc, char* argv[])
{
// call with a function pointer
func(foo);
func_wrapper(foo); // error
// call with a member …Run Code Online (Sandbox Code Playgroud) 我个人实验之一,了解一些C++ 0x特性:我正在尝试将函数指针传递给模板函数来执行.最终执行应该发生在不同的线程中.但是对于所有不同类型的函数,我无法使模板工作.
#include <functional>
int foo(void) {return 2;}
class bar {
public:
int operator() (void) {return 4;};
int something(int a) {return a;};
};
template <class C>
int func(C&& c)
{
//typedef typename std::result_of< C() >::type result_type;
typedef typename std::conditional<
std::is_pointer< C >::value,
std::result_of< C() >::type,
std::conditional<
std::is_object< C >::value,
std::result_of< typename C::operator() >::type,
void>
>::type result_type;
result_type result = c();
return result;
}
int main(int argc, char* argv[])
{
// call with a function pointer
func(foo);
// call with a …Run Code Online (Sandbox Code Playgroud)