C++:具有可变模板的成员函数指针参数

C++: Member function pointer parameter with variadic template

本文关键字:成员 函数 指针 参数 C++      更新时间:2023-10-16

我正在为我正在进行的项目编写一个简短的单元测试程序。

如果给定函数抛出某种异常,则此测试通过:

template <class Exception, class Return, class... Args>
bool throws(std::string s, Return(*fn)(Args...), Args... args)
{
  try
  {
    fn(args...);
  }
  catch (Exception& e)
  {
    return output(s, true);
  }
  catch (...)
  {
    return output(s, false);
  }
  return output(s, false);
}

使用它看起来像这样:

throws<int>("Testing: Throws int", &foo);

这很管用。但现在我正在尝试编写一个类似的函数,它将与类的成员函数一起使用。

到目前为止,我得到的是:

template <class Exception, class Object, class Return, class... Args>
bool throws(std::string s, Object& o, Return(Object::*fn)(Args...), Args... args)
{
  // ...
  (o.*fn)(args...);
  // ...
}
template <class Exception, class Object, class Return, class... Args>
bool throws(std::string s, const Object& o, Return(Object::*fn)(Args...) const, Args... args)
{ /* ... */ }

使用时应该是这样的:

Bar<int> b1(5), b2(2);
typedef Bar<int>& (Bar<int>::*fn)(const Bar<int>&);
typedef Bar<int> (Bar<int>::*const_fn)(const Bar<int>&) const;
fn foo = &Bar<int>::foo;
const_fn const_foo = &Bar<int>::const_foo;
throws<int>("Testing member function", b1, foo, b2);
throws<int>("Testing const member function", b1, const_foo, b2);

但当我这样做时,我会得到这两个throws((函数的"无匹配函数调用"。具体而言:

error: no matching function for call to ‘throws(const char [30], Bar<int>&, Bar<int> (Bar<int>::*&)(const Bar<int>&)const, const Bar<int>&)

非常量版本也有类似的版本。

我注意到一些const是不同的,所以我尝试在使用函数的地方放入一些const_cast,但没有骰子。这是我第一次使用成员函数指针和可变模板(根本不用说同时使用了(,所以我肯定会错过一些东西。。。有比我更有经验的人有什么想法吗?

我唯一无法解释的是(Bar<int>::*&)。&最后与呼叫不匹配。。。但这应该不是问题吧?

编辑:根据要求,我的酒吧类别:

template <class T>
class Bar
{
private:
  T _data;
public:
  Bar (const int i) : _data(i) {}
  Bar const_foo (const Bar& other) const
  {
    if (_data != other._data)
    {
      throw _data;
    }
    return Bar(_data);
  }
  Bar& foo (const Bar& other)
  {
    if (_data != other._data)
    {
      throw _data;
    }
    return *this;
  }
};

以及全部错误:

test.cpp: In function ‘int main()’:
test.cpp:111:53: error: no matching function for call to ‘throws(const char [24], Bar<int>&, Bar<int>& (Bar<int>::*&)(const Bar<int>&), Bar<int>&)’
test.cpp:111:53: note: candidates are:
test.cpp:31:6: note: template<class Exception, class Return, class ... Args> bool throws(std::string, Return (*)(Args ...), Args ...)
test.cpp:53:6: note: template<class Exception, class Object, class Return, class ... Args> bool throws(std::string, Object&, Return (Object::*)(Args ...), Args ...)
test.cpp:75:6: note: template<class Exception, class Object, class Return, class ... Args> bool throws(std::string, const Object&, Return (Object::*)(Args ...)const, Args ...)
test.cpp:112:65: error: no matching function for call to ‘throws(const char [30], Bar<int>&, Bar<int> (Bar<int>::*&)(const Bar<int>&)const, Bar<int>&)’
test.cpp:112:65: note: candidates are:
test.cpp:31:6: note: template<class Exception, class Return, class ... Args> bool throws(std::string, Return (*)(Args ...), Args ...)
test.cpp:53:6: note: template<class Exception, class Object, class Return, class ... Args> bool throws(std::string, Object&, Return (Object::*)(Args ...), Args ...)
test.cpp:75:6: note: template<class Exception, class Object, class Return, class ... Args> bool throws(std::string, const Object&, Return (Object::*)(Args ...)const, Args ...)

我注意到您使用的是非成员版本,该版本的函数不带任何参数。我相信当你试图传递参数时,你的代码会失败,而不是当你把它改为使用成员函数时。通过将其更改为采用成员函数并在同一步骤中传递参数,您混淆了失败的真正原因。我相信,如果您尝试使用零参数的成员函数,您的代码会起作用。我相信,如果你试图将非成员版本与接受一个或多个参数的函数一起使用,它会失败。

template <class Exception, class Object, class Return, class... Args>
bool throws(std::string s, Object& o, Return(Object::*fn)(Args...), Args... args)

在这里,您告诉编译器throws的第四个参数和函数指针的第一个参数的类型必须完全相同。

error: no matching function for call to ‘throws(const char [24], Bar<int>&,
       Bar<int>& (Bar<int>::*&)(const Bar<int>&), Bar<int>&)’

编译器告诉您,throws的第四个参数和函数指针的第一个参数不同,因此找不到匹配的throws声明。您的模板无法匹配,因为这意味着Args...必须同时推断为const Bar<int>&Bar<int>&,而这是两种不同的类型。

@WhozCraig在评论中提供的解决方案之所以有效,是因为函数类型和参数类型是独立推导的。他的解决方案通过通用引用传递函数指针,但这是偶然的;它也可以通过const左值引用或通过值获取函数指针。

template <class Exception, class Func, class... Args>
bool throws(std::string s, Func fn, Args&&... args)
{ /*...*/ }
template <class Exception, class Object, class MemFunc, class... Args>
bool throws(std::string s, Object& o, MemFunc fn, Args&&... args)
{ /*...*/ }

不幸的是,使用这种方法,您可能会遇到过载不明确的情况。对此,简单的解决方案是将成员版本重命名为memberThrows。更复杂的版本是使用标记调度或SFINAE(在这种情况下,我更喜欢前者(。

template <class Exception, class Func, class... Args>
bool throwsHelper(std::false_type, std::string s, Func fn, Args&&... args)
{ /*...*/ }
template <class Exception, class MemFunc, class Object, class... Args>
bool throwsHelper(std::true_type, std::string s, MemFunc fn, Object&& o, Args&&... args)
{ /*...*/ }
template <class Exception, class Func, class... Args>
bool throws(std::string s, Func fn, Args&&... args)
{
    using isMember = typename std::is_member_pointer<Func>::type;
    return throwsHelper(isMember(), s, fn, std::forward<Args>(args)...);
}

请注意,我必须将参数的顺序更改为成员版本,以便在这两种情况下都允许统一调用。

确保Bar<int>::const_fooconst成员函数,并且它具有返回类型Bar<int>(而不是引用(。如果这不是问题所在,请发布更多代码。