C++11 的 std::ref 用法

C++11 中的 std::ref 用于确保函数参数按引用传递,特别是在 std::bind 和多线程的 std::thread 中。示例展示了当不使用 std::ref 时,参数会被拷贝,导致预期的修改无法体现在原始变量上。std::ref 的使用确保了对原变量的直接操作,这对于需要修改传入参数的场景至关重要。
摘要由CSDN通过智能技术生成

C++11 中引入 std::ref 用于取某个变量的引用,这个引入是为了解决一些传参问题。

我们知道 C++ 中本来就有引用的存在,为何 C++11 中还要引入一个 std::ref 了?主要是考虑函数式编程(如 std::bind)在使用时,是对参数直接拷贝,而不是引用。下面通过例子说明

示例1:

#include <iostream>
#include <functional>


void f(int& n1, int& n2, const int& n3)
{
    std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    ++n1; // increments the copy of n1 stored in the function object
    ++n2; // increments the main()'s n2
    // ++n3; // compile error
}

int main()
{
    int n1 = 1, n2 = 2, n3 = 3;
    std::function<void()> bound_f = std::bind(f, n1, std::ref(n2), std::cref(n3));
    n1 = 10;
    n2 = 11;
    n3 = 12;
    std::cout << "Before function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
    bound_f();
    std::cout << "After function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
}

输出:

Before function: 10 11 12
In function: 1 11 12
After function: 10 12 12

上述代码在执行 std::bind 后,在函数 f() 中 n1 的值仍然是 1,n2 和 n3 改成了修改的值,说明 std::bind 使用的是参数的拷贝而不是引用,因此必须显示利用 std::ref 来进行引用绑定。具体为什么 std::bind 不使用引用,可能确实有一些需求,使得 C++11 的设计者认为默认应该采用拷贝,如果使用者有需求,加上 std::ref 即可。

示例2:

#include <iostream>
#include <thread>
#include <string>


void threadFunc(std::string& str, int a)
{
    str = "change by threadFunc";
    a = 13;
}

int main()
{
    std::string str("main");
    int a = 9;
    std::thread th(threadFunc, std::ref(str), a);

    th.join();

    std::cout << "str = " << str << std::endl;
    std::cout << "a = " << a << std::endl;

    return 0;
}

输出:

str = change by threadFunc
a = 9

可以看到,和 std::bind 类似,多线程的 std::thread 也是必须显式通过 std::ref 来绑定引用进行传参,否则,形参的引用声明是无效的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在C++中,std::promise是一种线程间通信机制,它允许一个线程在另一个线程中设置一个。在使用std::promise时,一个线程可以将一个传递给另一个线程,而无需等待该线程返回。具体而言,std::promise允许线程A创建一个std::future对象并将其返回给线程B,线程B可以在某个时间点通过调用std::promise的set_value()方法来设置std::future对象的。一旦std::future对象被设置了,线程A可以通过调用std::future对象的get()方法来获取该。在使用std::promise时,需要注意的是,一个std::promise对象只能被设置一次,因此在使用它时需要小心。 ### 回答2: std::promise 是C++标准库中的一个类,用于在线程之间传递异步操作的结果。它通常与 std::future 一起使用,通过 std::promise 传递数据给 std::future 对象,在另一个线程中可以获取到该数据。 std::promise 的用法可以分为三个步骤:创建 promise 对象、获取 future 对象、设置 promise 对象的。 首先,我们需要创建一个 promise 对象,这可以通过构造函数来完成,例如:std::promise<int> myPromise; 这样就创建了一个用于传递 int 类型数据的 promise 对象。 接下来,我们需要使用 promise 对象获取对应的 future 对象,通过调用 promise 对象的成员函数 std::promise::get_future() 来获取,例如:std::future<int> myFuture = myPromise.get_future(); 这样就获得了一个用于接收 int 类型数据的 future 对象。 最后,我们可以通过设置 promise 对象的来传递数据给 future 对象。这可以通过调用 promise 对象的成员函数 std::promise::set_value() 来完成,例如:myPromise.set_value(42); 这样就将 42 传递给了之前创建的 future 对象。 在另一个线程中,我们可以通过调用 future 对象的成员函数 std::future::get() 来获取到设置的,例如:int result = myFuture.get(); 这样就可以获取到之前设置的 42。 需要注意的是,一旦 promise 对象被设置了,就不能再次设置。如果我们不希望在某些情况下传递数据到 future 对象,可以使用 std::promise::set_exception() 来设置一个异常。 总而言之,std::promise 提供了一种方便的机制来在线程之间传递异步操作的结果,通过设置 promise 对象的,可以在另一个线程中获取到这些,并进行相应的处理。 ### 回答3: std::promise是C++标准库中的一个类模板,用于在多线程编程中用来存放一个或者一个异常的容器。它通常与std::future一起使用,将一个或异常从一个线程传递到另一个线程。 std::promise提供了两个主要的成员函数set_value()和set_exception()来设置和异常。当一个线程调用set_value()或set_exception()时,std::promise的状态会变为“就绪”状态。其他线程可以通过std::future对象获取std::promise中的或异常。 使用std::promise的基本步骤如下: 1. 创建一个std::promise对象,例如std::promise<int> p; 2. 创建一个std::future对象,通过std::promise的get_future()成员函数获取,例如std::future<int> f = p.get_future(); 3. 启动一个线程,该线程可以在某个时间点调用p.set_value()或p.set_exception()来设置或异常。 4. 在需要的地方,通过std::future对象的get()成员函数获取std::promise中的或异常。 例如,通过std::promise实现一个计算线程和一个读取结果的线程的示例: ``` #include <iostream> #include <thread> #include <functional> #include <future> void calculate(std::promise<int>& resultPromise, int a, int b) { int result = a + b; resultPromise.set_value(result); } int main() { std::promise<int> resultPromise; std::future<int> resultFuture = resultPromise.get_future(); std::thread calculationThread(calculate, std::ref(resultPromise), 3, 4); // 等待计算线程完成 calculationThread.join(); // 读取结果 int result = resultFuture.get(); std::cout << "Result: " << result << std::endl; return 0; } ``` 这个示例中,计算线程通过调用resultPromise.set_value()来设置结果,读取结果的线程通过resultFuture.get()来阻塞等待结果的返回,这样就实现了两个线程之间的的传递和同步。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值