Perfect forwarding a member of object

后端 未结 2 1980
甜味超标
甜味超标 2020-12-09 04:25

Suppose I have two structs:

struct X {};
struct Y { X x; }

I have functions:

void f(X&);
void f(X&&         


        
相关标签:
2条回答
  • 2020-12-09 05:02
    template <typename T>
    void g(T&& t) {
      f(std::forward<T>(t).x);
    }
    
    0 讨论(0)
  • 2020-12-09 05:16

    I think this will work, although I'm not sure:

    template<class T, class M>
    struct mforward {
      using type = M&&; 
    };
    template<class T, class M>
    struct mforward<T&, M> {
      using type = M&; 
    };
    
    template <typename T>
    void g(T&& t) {
      f(std::forward<typename mforward<T, decltype(t.x)>::type>(t.x));
    }
    
    0 讨论(0)
提交回复
热议问题