Typedef 用于指向 cv 和/或 ref 限定成员函数的指针

Typedef for a pointer to a cv- and/or ref-qualified member function

本文关键字:成员 函数 指针 ref 用于 cv Typedef      更新时间:2023-10-16
struct foo {
    void bar(int&&) && { }
};
template<class T>
using bar_t = void (std::decay_t<T>::*)(int&&) /* add && if T is an rvalue reference */;
int main()
{
    using other_t = void (foo::*)(int&&) &&;
    static_assert(std::is_same<bar_t<foo&&>, other_t>::value, "not the same");
    return 0;
}

我想要那个

  • 如果T = foobar_t<T>产生void (foo::*)(int&&)
  • 如果T = foo constbar_t<T>产生void (foo::*)(int&&) const
  • 如果T = foo&bar_t<T>产生void (foo::*)(int&&) &
  • 如果T = foo const&bar_t<T>产生void (foo::*)(int&&) const&

等等。我怎样才能做到这一点?

这应该可以完成这项工作:

template <typename, typename T> struct add {using type = T;};
template <typename F, typename C, typename R, typename... Args>
struct add<F const, R (C::*)(Args...)> {using type = R (C::*)(Args...) const;};
template <typename F, typename C, typename R, typename... Args>
struct add<F&, R (C::*)(Args...)> :
  std::conditional<std::is_const<F>{}, R (C::*)(Args...) const&,
                                       R (C::*)(Args...) &> {};
template <typename F, typename C, typename R, typename... Args>
struct add<F&&, R (C::*)(Args...)> :
  std::conditional<std::is_const<F>{}, R (C::*)(Args...) const&&,
                                       R (C::*)(Args...) &&> {};

演示。请注意,F的基础类型将被忽略。