【问题标题】:How to get reference to an element of a std::tuple?如何获取对 std::tuple 元素的引用?
【发布时间】:2013-03-27 23:26:34
【问题描述】:

您可以使用std::get<n>(tuple) 获取std::tuplenth 元素的值。但我需要将该元组的一个元素作为对函数的引用传递。

如何获取对 std::tuple 元素的引用?

【问题讨论】:

    标签: c++ reference stdtuple


    【解决方案1】:

    std::get 返回一个引用(常量或非常量),所以这可行:

    void fun(int &a) {
        a = 15;
    }
    
    void test() {
        std::tuple<int, char> foo{ 12, 'a' };
        fun(std::get<0>(foo));
    }
    

    演示here.

    【讨论】:

      【解决方案2】:

      get 根据参数的类型返回引用、右值引用或常量引用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-19
        • 2010-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多