简单的模板推导错误

simple template deduction error

本文关键字:错误 简单      更新时间:2023-10-16

我正在尝试使用一些模板自动生成const成员函数对。请帮我编译这个代码。

template<typename T>
struct Constifier
{
  typedef T Type;
};
template<typename T>
struct Constifier<T &>
{
  typedef const T &Type;
};
template<typename T>
struct Constifier<T &&>
{
  typedef const T &&Type;
};
template<typename T>
struct Constifier<T *>
{
  typedef const T *Type;
};
template<typename F>
struct ReturnType;
template<typename R, typename ...Ts>
struct ReturnType<R (*)(Ts ...ts)>
{
  typedef R Type;
};
template<typename R, typename T, typename ...Ts>
struct ReturnType<R (T::*)(Ts ...ts)>
{
  typedef R Type;
};
template<typename T, typename F, typename ...Ts>
auto callConstVersion(const T *t, F f, Ts ...ts)
{
  return const_cast<typename Constifier<typename ReturnType<F>::Type>::Type>((const_cast<T *>(t)->*f)(ts...));
}
struct A
{
  A *p;
  A *get() {return p;}
  const A *get() const {return callConstVersion(this, &A::get);}
};

get成员函数重载,因此,给定&A::get作为参数,F变得不可推导。要消除两者之间的歧义,请使用static_cast:

static_cast<const A*(A::*)() const>(&A::get) // for the const overload
static_cast<A*(A::*)()>(&A::get) // for the non-const overload

或在模板参数列表中显式指定F

callConstVersion<A,A*(A::*)()>(this, &A::get);

或者,如果你知道callConstVersion应该总是采用一个非常常量的非易失性非ref限定成员函数,你可以如下重新定义它:

template<typename T, typename R, typename... Args, typename ...Ts>
auto callConstVersion(const T *t, R(T::*f)(Args...), Ts ...ts)

(那么普通callConstVersion(this, &A::get)应该可以工作(。