c++中ref的作用示例解析

正文

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

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

示例1:

#include <functional>
#include <iostream>
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 即可。

#include <thread>
#include <iostream>
#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;
}

该程序创建一个线程 th,调用带有两个参数的 threadFunc 函数:一个是 std::string 对象 str 的引用,另一个是整数 a。函数 threadFunc 修改字符串 str 为 “change by threadFunc”,但不修改整数 a。最后在主线程中输出 stra 的值。

输出:

str = change by threadFunc
a = 9

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

总结

std::ref 是一个 C++ 标准库函数模板,它将对象的引用转换为可复制的可调用对象。

std::ref 用于将对象的引用传递给函数或线程等可调用对象的参数。如果不使用 std::ref,那么函数或线程会将对象的副本传递给可调用对象的参数,这可能会导致无法预期的结果,因为对该副本的修改不会影响原始对象。通过使用 std::ref,可以确保可调用对象引用的是原始对象,因此对该对象的修改将影响原始对象。

需要注意的是,使用 std::ref 前必须确保原始对象的生命周期至少与可调用对象相同,否则会导致悬空引用。另外,std::ref 不能用于将指向临时对象或将过时对象的引用传递给可调用对象。

总之,std::ref 的作用是将对象的引用转换为可复制的可调用对象,使得在函数或线程等可调用对象中引用原始对象,而不是其副本。

以上就是c++中ref的作用示例解析的详细内容,更多关于c++ ref作用的资料请关注aitechtogether.com其它相关文章!

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年5月13日
下一篇 2023年5月13日

相关推荐