C++ 模板、函数指针和C++;0x
为了理解C++0x的一些特性,我个人做了一个实验:我试图将函数指针传递给要执行的模板函数。最终,执行应该发生在不同的线程中。但是由于函数类型不同,我无法让模板正常工作C++ 模板、函数指针和C++;0x,c++,templates,c++11,function-pointers,C++,Templates,C++11,Function Pointers,为了理解C++0x的一些特性,我个人做了一个实验:我试图将函数指针传递给要执行的模板函数。最终,执行应该发生在不同的线程中。但是由于函数类型不同,我无法让模板正常工作 #include <functional> int foo(void) {return 2;} class bar { public: int operator() (void) {return 4;}; int something(int a) {return a;}; }; template
#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 member function
bar b;
func(b);
// call with a bind expression
func(std::bind(&bar::something, b, 42));
// call with a lambda expression
func( [](void)->int {return 12;} );
return 0;
}
#包括
int foo(void){return 2;}
分类栏{
公众:
int运算符()(void){return 4;};
int某物(inta){返回a;};
};
模板
int func(C&&C)
{
//typedef typename std::result_of::type result_type;
typedef typename std::conditional<
std::is_指针::值,
std::result_of::type,
条件的<
std::is_object::值,
::type的std::result_,
无效>
>::类型结果\类型;
结果_type result=c();
返回结果;
}
int main(int argc,char*argv[])
{
//使用函数指针调用
func(foo);
//使用成员函数调用
b栏;
func(b);
//使用绑定表达式调用
func(std::bind(&bar::something,b,42));
//使用lambda表达式调用
func([](void)->int{return 12;});
返回0;
}
仅模板的结果_似乎无法在类栏中找到运算符(),并且我创建的笨拙条件没有编译。有什么想法吗?常量函数是否会有其他问题?使用
decltype
如何
template <class C>
auto func(C&& c) -> decltype(c()) {
auto result = c();
return result;
}
模板
自动函数(C&&C)->decltype(C()){
自动结果=c();
返回结果;
}
如果我正确理解C++0x草案,那么以下内容实际上就足够了:
typedef typename std::result_of<C()>::type result_type;
typedef typename std::result\u of::type result\u type;
使用它而不是你的条件表达式,它在gcc4.5上编译得很好-也许你在你正在使用的任何编译器中都发现了一个bug?我得到了你的模板来实例化,但是GCC抱怨可能每次都使用了
的结果
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,
// C++0x still requires "typename" sprinkles:
typename std::result_of< C() >::type,
typename std::conditional<
std::is_object< C >::value,
// result_of takes a *type* as an argument, not an object:
//typename std::result_of< decltype( &C::operator() ) >::type,
// Or better:
typename std::result_of< C >::type,
void>
>::type result_type;
result_type result = c();
return result;
}
int main(int argc, char* argv[])
{
// according to GCC, func(foo) passes a function reference.
func(foo);
模板
int func(C&&C)
{
//typedef typename std::result_of::type result_type;
typedef typename std::conditional<
std::is_指针::值,
//C++0x仍然需要“typename”标记:
typename std::result_of::type,
类型名称std::条件<
std::is_object::值,
//的结果_将*类型*作为参数,而不是对象:
//:type的typename std::result_,
//或者更好:
typename std::result_of::type,
无效>
>::类型结果\类型;
结果_type result=c();
返回结果;
}
int main(int argc,char*argv[])
{
//根据GCC,func(foo)传递函数引用。
func(foo);
第一条错误消息:
rof.cpp:23:17: error: invalid use of incomplete type 'struct std::result_of<int (&)()>'
rof.cpp:23:17:错误:对不完整类型“struct std::result_of”的使用无效
的result\u是按照标准中的规定实现的,因此GCC似乎无法匹配部分专门化声明中的伪原型语法。它不编译的原因可能是因为func
返回int
而不是result\u type
(本例中的int
在所有情况下都是int,但可能仍然会导致问题)?@Chris:不,这不是原因。@KennyTM-我认为不是,但仍然认为这是问题。它没有编译的原因是打字错误。当你指小写的C
时,你输入了大写的C
(这是类)(这是函数的参数)。您仍然得到了一些答案,这些答案可能会使您的代码更简洁,因此我不想将其作为答案发布。@Chris,我不认为这会有什么问题?对函数的引用应该起作用:“Fn应该是函数对象类型(20.8)、对函数的引用或对函数对象类型的引用。”@gf:好吧,我会让这个注释很有学究味。不管怎样,GCC的错误消息清楚地表明它拒绝了好的代码。这也是我的想法,但它没有编译。整个复杂条件就是为了解决这个问题而创建的。@user328543:如果我注释掉你的条件并使用之前注释掉的行,它会编译绝对值在gcc4.5上非常好。看起来干净多了。谢谢!