Loki的Functor与可变模板 [英] Loki's Functor with variadic templates

查看:147
本文介绍了Loki的Functor与可变模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于 Functor 库的实现 Loki 的问题。
我正在做一些更改,以使其与可变参数模板,而不是具有行和线的模板专业化。问题是,我试图使用typedef的可变参数模板,我不明白我的错误,这就是为什么我可以使用一些来自专家的帮助...



< a href =https://gist.github.com/athanase/7978090 =nofollow>这里是头文件。



I用一个简单的例子测试它:

  static void foo()
{
std :: cout ;& foo !!! << std :: endl;
}
int
main(int argc,const char ** argv)
{
Functor< void,void& static_func(foo);
static_func();
}

这会给我这个错误

  /home/test/src/EntryPoint.cpp:237:17:error:没有匹配的调用'(Functor< void,void>)()'
在/home/test/src/EntryPoint.cpp:231:0中包含的文件中:
/home/test/include/FunctorTest.h:217:7:note:candidate is:
/ home /test/include/FunctorTest.h:292:16:note:Functor< R,TList> :: ResultType Functor< R,TList> :: operator()(Functor< R,TList> :: MyList&& [with R = void; TList = {void}; Functor< R,TList> :: ResultType = void; functor< R,TList> :: MyList = variadic_typedef< void>]
/home/test/include/FunctorTest.h:292:16:note:candidate expects 1 argument,0 provided
/ home / test / src / EntryPoint.cpp:在全局范围:
/home/test/src/EntryPoint.cpp:234:1:warning:未使用的参数'argc'[-Wunused-parameter]
/ home /test/src/EntryPoint.cpp:234:1:warning:unused参数'argv'[-Wunused-parameter]
在/home/test/src/EntryPoint.cpp:231:0中包含的文件:
/home/test/include/FunctorTest.h:在FunctorHandler< ParentFunctor,Fun>的实例化中::: ResultType FunctorHandler< ParentFunctor,Fun> :: operator()(FunctorHandler< ParentFunctor,Fun> :: MyList&& ;)[with ParentFunctor = Functor< void,void> ;; Fun = void(*)(); FunctorHandler< ParentFunctor,Fun> :: ResultType = void; FuncterHandler< ParentFunctor,Fun> :: MyList = variadic_typedef< void>]':
/home/test/src/EntryPoint.cpp:247:1:从这里需要
/ home / test / include / FunctorTest.h:159:49:error:没有匹配的函数用于调用'forward(FunctorHandler< Functor< void,void> ;, void(*)()> :: MyList&)'
/ home / test /include/FunctorTest.h:159:49:注意:候选项是:
在/usr/include/c++/4.7/bits/stl_pair.h:61:0中包含的文件中,
从/ usr /include/c++/4.7/utility:72,
从/home/jean/Lib/vitals/include/CLPair.h:28,
从/ home / jean / Lib / vitals / include / CLMap .h:27,
来自/home/jean/Lib/vitals/include/HTCmdLineParser.h:27,
来自/home/test/include/EntryPoint.h:23,
从/home/test/src/EntryPoint.cpp:22:
/usr/include/c++/4.7/bits/move.h:77:5:note:template< class _Tp> constexpr _Tp&& std :: forward(typename std :: remove_reference< _From> :: type&)
/usr/include/c++/4.7/bits/move.h:77:5:note:template argument deduction / substitution failed:
在/home/test/src/EntryPoint.cpp:231:0中包含的文件:
/home/test/include/FunctorTest.h:159:49:note:无法转换'parms'(类型'FunctorHandler< Functor< void,void>,void(*)()> :: MyList {aka variadic_typedef< void>}')来键入'std :: remove_reference< convert_in_tuple< variadic_typedef< void& > > :: type& {aka convert_in_tuple< variadic_typedef< void> >&}&}'


解决方案

一个tuple函数直接使用std :: forward参数。
您需要这样的:

  / * 
*指数< N + 1& ==>指数< 0,0,1,2,...,N> ==> Seq< 0,1,2,...,N>
* /
template< int ...> struct Seq {};

template< int N,int ... S>
struct索引:索引< N-1,N-1,S ...> {};

template< int ... S>
struct Indices< 0,S ...> {
typedef Seq< S ...> SeqType;
};

template< typename R,typename F,typename ... A,int ... S>
inline R Apply(F f,const std :: tuple< A ...>& t,Seq< S ...>){
return(R)f get(S(t)...);
}

template< typename R,typename F,typename ... A>
inline R Apply(F f,const std :: tuple< A ...>& t){
return Apply< R>(f,t,typename Indices< sizeof ... A)> SeqType());
}

template< typename R,typename T,typename F,typename ... A,int ... S>
inline R Apply(T * obj,F f,const std :: tuple< A ...>& t,Seq< S ...>){
return obj- * f)(std :: get S(t)...);
}

template< typename R,typename T,typename F,typename ... A>
inline R Apply(T * obj,F f,const std :: tuple< A ...>& t){
return Apply< R(obj,f,t,typename Indices< ; sizeof ...(A)> :: SeqType());
}

查看我的基于可变参数模板的Closure实现: https://github.com/idealvin/base/blob/master/closure.h p>

I have a question about the Functor implementation of the library Loki. I am doing some changes in order to make it work with variadic templates instead of having lines and lines of template specialization. The problem is that I am trying to use typedef for variadic template and I do not understand my error, that is why I could use some help from experts...

Here is the header file.

I tested it with a simple example:

static void foo()
{
    std::cout << "foo !!!" << std::endl;
}
int
main( int argc, const char** argv )
{
    Functor<void, void> static_func(foo);
    static_func();
}

Which gives me this error

/home/test/src/EntryPoint.cpp:237:17: error: no match for call to ‘(Functor<void, void>) ()’
    In file included from /home/test/src/EntryPoint.cpp:231:0:
    /home/test/include/FunctorTest.h:217:7: note: candidate is:
    /home/test/include/FunctorTest.h:292:16: note: Functor<R, TList>::ResultType Functor<R, TList>::operator()(Functor<R, TList>::MyList&&) const [with R = void; TList = {void}; Functor<R, TList>::ResultType = void; Functor<R, TList>::MyList = variadic_typedef<void>]
    /home/test/include/FunctorTest.h:292:16: note:   candidate expects 1 argument, 0 provided
    /home/test/src/EntryPoint.cpp: At global scope:
    /home/test/src/EntryPoint.cpp:234:1: warning: unused parameter ‘argc’ [-Wunused-parameter]
    /home/test/src/EntryPoint.cpp:234:1: warning: unused parameter ‘argv’ [-Wunused-parameter]
    In file included from /home/test/src/EntryPoint.cpp:231:0:
    /home/test/include/FunctorTest.h: In instantiation of ‘FunctorHandler<ParentFunctor, Fun>::ResultType FunctorHandler<ParentFunctor, Fun>::operator()(FunctorHandler<ParentFunctor, Fun>::MyList&&) [with ParentFunctor = Functor<void, void>; Fun = void (*)(); FunctorHandler<ParentFunctor, Fun>::ResultType = void; FunctorHandler<ParentFunctor, Fun>::MyList = variadic_typedef<void>]’:
    /home/test/src/EntryPoint.cpp:247:1:   required from here
    /home/test/include/FunctorTest.h:159:49: error: no matching function for call to ‘forward(FunctorHandler<Functor<void, void>, void (*)()>::MyList&)’
    /home/test/include/FunctorTest.h:159:49: note: candidates are:
    In file included from /usr/include/c++/4.7/bits/stl_pair.h:61:0,
                     from /usr/include/c++/4.7/utility:72,
                     from /home/jean/Lib/vitals/include/CLPair.h:28,
                     from /home/jean/Lib/vitals/include/CLMap.h:27,
                     from /home/jean/Lib/vitals/include/HTCmdLineParser.h:27,
                     from /home/test/include/EntryPoint.h:23,
                     from /home/test/src/EntryPoint.cpp:22:
    /usr/include/c++/4.7/bits/move.h:77:5: note: template<class _Tp> constexpr _Tp&& std::forward(typename std::remove_reference<_From>::type&)
    /usr/include/c++/4.7/bits/move.h:77:5: note:   template argument deduction/substitution failed:
    In file included from /home/test/src/EntryPoint.cpp:231:0:
    /home/test/include/FunctorTest.h:159:49: note:   cannot convert ‘parms’ (type ‘FunctorHandler<Functor<void, void>, void (*)()>::MyList {aka variadic_typedef<void>}’) to type ‘std::remove_reference<convert_in_tuple<variadic_typedef<void> > >::type& {aka convert_in_tuple<variadic_typedef<void> >&}’

解决方案

You can't apply a tuple to function parameters with std::forward directly. You need something like this:

/*
 * Indices<N + 1> ==> Indices<0, 0, 1, 2, ..., N> ==> Seq<0, 1, 2, ... , N>
 */
template<int...> struct Seq {};

template<int N, int ... S> 
struct Indices : Indices<N - 1, N - 1, S...> {};

template<int ... S> 
struct Indices<0, S...> { 
  typedef Seq<S...> SeqType; 
};

template<typename R, typename F, typename ... A, int ... S>
inline R Apply(F f, const std::tuple<A...>& t, Seq<S...>) {
  return (R) f(std::get<S>(t)...);
}

template<typename R, typename F, typename ... A>
inline R Apply(F f, const std::tuple<A...>& t) {
  return Apply<R>(f, t, typename Indices<sizeof...(A)>::SeqType());
}

template<typename R, typename T, typename F, typename ... A, int ... S>
inline R Apply(T* obj, F f, const std::tuple<A...>& t, Seq<S...>) {
  return (R) (obj->*f)(std::get<S>(t)...);
}

template<typename R, typename T, typename F, typename ... A>
inline R Apply(T* obj, F f, const std::tuple<A...>& t) {
  return Apply<R>(obj, f, t, typename Indices<sizeof...(A)>::SeqType());
}

See my implementation about Closure based on variadic templates: https://github.com/idealvin/base/blob/master/closure.h

这篇关于Loki的Functor与可变模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆