簡體   English   中英

Typedef指向cv和/或ref限定成員函數的指針

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

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

演示 請注意,忽略F的基礎類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM