c++ - 指向 cv 和/或 ref 限定成员函数的指针的 Typedef

标签 c++ templates function-pointers c++14 pointer-to-member

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;
}

我想要那个

  • bar_t<T>产量 void (foo::*)(int&&)如果T = foo
  • bar_t<T>产量 void (foo::*)(int&&) const如果T = foo const
  • bar_t<T>产量 void (foo::*)(int&&) &如果T = foo&
  • bar_t<T>产量 void (foo::*)(int&&) const&如果T = foo 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...) &&> {};

Demo .请注意,F 的基础类型将被忽略。

关于c++ - 指向 cv 和/或 ref 限定成员函数的指针的 Typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36071136/

相关文章:

c++ - 如何从 Windows 中的 session ID 获取用户名/SID?

c++ - yaml-cpp 的主要性能问题

c++ - 嵌套模板的可能性

c++ - 当类型包含具有给定名称和类型的静态变量时启用_if 函数

c++ - 将函数指针作为函数参数传递的目的

c++ - 访问类中指针的 vector

C++ 并行矩阵乘法,计算不正确

javascript - Extjs 4.2.1 XTemplate 子元素的日期格式仅在 IE 上显示 NaN

c++ - 如何获得指向 Eigen operator() 的函数指针

c - 抽象函数指针