std::make_tuple doesn't make references

后端 未结 6 815
名媛妹妹
名媛妹妹 2020-12-04 23:53

I\'ve been experimenting with std::tuple in combination with references:

#include 
#include 

int main() {
  int a,         


        
相关标签:
6条回答
  • 2020-12-05 00:19

    Try forward_as_tuple:

    auto test3 = std::forward_as_tuple(ar,br);
    
    0 讨论(0)
  • 2020-12-05 00:20

    In C++14, you can proceed like that:

    template<typename ...T, size_t... I>
    auto make_rtuple_helper(std::tuple<T...>& t ,  std::index_sequence<I...>)
    -> std::tuple<T&...>
    { return std::tie(std::get<I>(t)...) ;}
    
    template<typename ...T>
    std::tuple<T&...> make_rtuple( std::tuple<T...>& t )
    {
        return make_rtuple_helper( t, std::make_index_sequence<sizeof...(T)>{});
    }
    

    See the how it works here in coliru : http://coliru.stacked-crooked.com/a/a665130e17fd8bcc

    Cheers A.A.

    0 讨论(0)
  • 2020-12-05 00:21

    How about:

    auto test3 = std::make_tuple(std::ref(a),std::ref(b));
    
    0 讨论(0)
  • 2020-12-05 00:33

    std::tie makes non-const references.

    auto ref_tuple = std::tie(a,b); // decltype(ref_tuple) == std::tuple<int&, int&>
    

    For const references, you'll either want the std::cref wrapper function:

    auto cref_tuple = std::make_tuple(std::cref(a), std::cref(b));
    

    Or use a simply as_const helper to qualify the variables before passing them off to std::tie:

    template<class T>
    T const& as_const(T& v){ return v; }
    
    auto cref_tuple = std::tie(as_const(a), as_const(b));
    

    Or, if you want to get fancy, write your own ctie (reusing std::tie and as_const):

    template<class... Ts>
    std::tuple<Ts const&...> ctie(Ts&... vs){
      return std::tie(as_const(vs)...);
    }
    
    auto cref_tuple = ctie(a, b);
    
    0 讨论(0)
  • 2020-12-05 00:37

    For the why: make_tuple parameters are passed by const reference (const T&), so if you pass int&, T matches int. If it deduced T to be int&, the parameter would be const T&&, and you'd get a compile error.

    0 讨论(0)
  • 2020-12-05 00:40

    What about std::make_tuple<int&, int&>(a, b);

    Admittedly, it kind of defeats the purpose, but for functions like make_shared you still get benefits.

    Warning, I haven't tried to compile this, but I believe it will work.

    0 讨论(0)
提交回复
热议问题