Nik*_*Nik 5 c++ templates functor
基于《现代 C++ 设计》一书中的第 5 章(广义函子),
我正在尝试编写一个 Functor 模板。在问我“为什么我不直接使用 Boost 的绑定或 Loki 之前?” 简单的回答是“因为我想学习”。
话虽如此,我已经按照本书进行操作,并使用示例代码作为参考,但我不断提出编译时错误,我似乎无法理解为什么。
当我修改本书的示例代码以不使用线程模型时,示例工作正常。但我的代码没有。这是相关代码:
非常基本的 TypeList,以及 TypeAt 和 Nulltype、EmptyType(也基于本书)
<TypeList.hpp>
---
class NullType
{};
class EmptyType
{};
#define TYPELIST_1(T1) TypeList<T1, NullType>
#define TYPELIST_2(T1,T2) TypeList<T1, TYPELIST_1(T2) >
#define TYPELIST_3(T1,T2,T3) TypeList<T1, TYPELIST_2(T2, T3) >
template < typename T, typename U >
struct TypeList
{
typedef T Head;
typedef U Tail;
};
namespace typelist
{
template <class TList, unsigned int i > struct TypeAt;
template <class Head, class Tail >
struct TypeAt<TypeList<Head, Tail>,0 >
{
typedef Head Result;
};
template <class Head, class Tail, unsigned int i >
struct TypeAt<TypeList<Head, Tail>, i >
{
typedef typename TypeAt<Tail, i-1>::Result Result;
};
template <class TList, unsigned int index,
typename DefaultType = NullType>
struct TypeAtNonStrict
{
typedef DefaultType Result;
};
}
---
Run Code Online (Sandbox Code Playgroud)
使用这些当然是函子
<Functor.hpp>
---
namespace functorPrivate
{
template<typename R>
struct FunctorBase
{
typedef R ResultType;
typedef EmptyType Param1;
private:
};
}
template <typename R, class TList>
class FunctorImpl;
template <typename R>
class FunctorImpl<R, NullType > : public functorPrivate::FunctorBase<R>
{
public:
typedef R ResultType;
virtual ~FunctorImpl(){}
virtual R apply() = 0;
};
template <typename R, typename P1>
class FunctorImpl<R, TYPELIST_1(P1) > : public functorPrivate::FunctorBase<R>
{
public:
typedef R ResultType;
typedef P1 Param1;
virtual ~FunctorImpl(){}
virtual R apply(P1) = 0;
};
template < class ParentFunctor, typename Fun>
class FunctionHandler : public ParentFunctor::Impl
{
private:
typedef typename ParentFunctor::Impl Base;
Fun fun;
public:
typedef typename Base::ResultType ResultType;
typedef typename Base::Param1 Param1;
FunctionHandler(const Fun& fun) :
fun(fun)
{}
virtual ~FunctionHandler()
{}
virtual ResultType apply()
{
return fun();
}
virtual ResultType apply(Param1 p1)
{
return fun(p1);
}
};
template <typename R, class TList = NullType>
class Functor
{
public:
typedef TList ParamList;
typedef R ResultType;
typedef FunctorImpl<R, TList> Impl;
typedef typename Impl::Param1 Param1;
Functor() :
impl(0)
{}
//Function / Handling functor type templated constructor
template<class Fun>
Functor(const Fun& fun) :
impl(new FunctionHandler< Functor, Fun>(fun))
{}
//apply functions
R apply()
{
return (*impl).apply();
}
R apply(Param1 p1)
{
return (*impl).apply(p1);
}
private:
std::auto_ptr<Impl> impl;
};
---
Run Code Online (Sandbox Code Playgroud)
当我尝试像这样使用它时:
---
class TestFunctor0
{
public:
int operator()()
{
cout << "zero param functor" << endl;
return 1;
}
};
//somewhere in main...
TestFunctor0 f;
Functor<int, NullType> command(f);
int res = command.apply();
CPPUNIT_ASSERT_EQUAL(1, res);
---
Run Code Online (Sandbox Code Playgroud)
我收到以下编译时错误:
../../../include/real/Functors.hpp: In member function ‘typename ParentFunctor::Impl::ResultType FunctionHandler<ParentFunctor, Fun>::apply(typename ParentFunctor::Impl::Param1) [with ParentFunctor = Functor<int, NullType>, Fun = TestFunctor0]’:
main.cpp:246: instantiated from here
../../../include/real/Functors.hpp:74: error: no match for call to ‘(TestFunctor0) (EmptyType&)’
main.cpp:31: note: candidates are: int TestFunctor0::operator()()
Run Code Online (Sandbox Code Playgroud)
有谁明白为什么我会看到这个问题?我没有想法。谢谢
这个问题已经解决了。事实证明,在 FunctionHandler 中,将 apply() 函数设置为虚拟会导致问题。一旦我删除了虚拟关键字,一切就顺利了。
我仍然不太清楚为什么这会如此彻底地改变事情,但它解决了问题。我对原因的最佳猜测是,通过添加 virtual,编译器将尝试使用正确的函数填充 vtable,并且由于 Param1 /技术上/由 NullType 定义,因此它会找到那个?
如果我错了,请纠正我,这只是我理解有限的最佳做法。