当前位置:网站首页>Stdref and stdcref
Stdref and stdcref
2022-07-02 02:38:00 【heater404】
std::ref and std::cref
1 std::ref and std::cref
c++11 Introduction in std::ref Used to get a reference to a variable , This introduction is to solve some parameter transfer problems .
- std::ref Used to wrap values passed by reference .
- std::cref For packing according to const Value passed by reference .
We know C++ There are references in , why C++11 There is another one introduced in std::ref Well ? It's mainly about functional programming ( Such as std::bind) When use , It's a direct copy of the parameters , Not references .
#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, char **)
{
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';
}
Output :
Before function: 10 11 12
In function: 1 11 12
After function: 10 12 12
The above code is executing std::bind after , In function f() in n1 The value of is still 1,n2 and n3 Changed to the modified value , explain std::bind A copy of the parameter is used instead of a reference , Therefore, it must be shown that std::ref For reference binding . Why exactly std::bind Do not use references , There may indeed be some needs , bring C++11 Our designers believe that copy should be adopted by default , If the user has a demand , add std::ref that will do .
Allied , also std::thread It must also be explicitly passed std::ref To bind references and pass parameters , otherwise , The reference declaration of formal parameters is invalid .
void foo(string &msg)
{
msg = "i am foo string";
}
string msg = "hello world";
thread *th = new thread(foo, std::ref(msg));
th->join();
std::cout << "msg:" << msg << endl;
2 std::bind
Can be std::bind Function as a general function adapter , It takes a callable object , Generate a new callable object to fit the parameter list of the original object .std::bind Bind the callable object with its parameters , The result after binding can be used std::function preservation .
2.1 Bind ordinary functions
#include <functional>
void foo(string &msg, int i)
{
msg = "i am foo:" + msg + to_string(i);
}
int main(int, char **)
{
auto f = std::bind(foo, std::placeholders::_1, 100);
string msg;
f(std::ref(msg));
std::cout << msg << endl; // i am foo:100
}
You can bind parameters when Binding , You can also occupy a seat first , Pass in parameters when executing .
2.2 Bind a member function
typedef struct
{
/* data */
void foo(string &msg, int i)
{
msg = "i am foo:" + msg + to_string(i);
}
} bar;
int main(int, char **)
{
bar b;
auto f = std::bind(bar::foo, &b, std::placeholders::_1, 100);
string msg;
f(std::ref(msg));
std::cout << msg << endl; // i am foo:100
}
For member functions , When Binding :
- The specified method must be displayed bar::foo.
- You must know which object this method belongs to , So the second parameter is the address of the object &b
2.3 std::thread
In the use of std::thread When , We also need to pay attention to the same problem .
typedef struct
{
/* data */
void foo(string &msg, int i)
{
msg = "i am foo:" + msg + to_string(i);
}
} bar;
int main(int, char **)
{
string msg;
bar b;
thread th(bar::foo, &b, std::ref(msg), 100);
th.join();
std::cout << msg << endl; // i am foo:100
}
边栏推荐
- JVM面试篇
- AcWing 245. Can you answer these questions (line segment tree)
- The basic steps of using information theory to deal with scientific problems are
- Coordinatorlayout + tablayout + viewpager2 (there is another recyclerview nested inside), and the sliding conflict of recyclerview is solved
- How to run oddish successfully from 0?
- No programming code technology! Four step easy flower store applet
- 【带你学c带你飞】2day 第8章 指针(练习8.1 密码开锁)
- Summary of some experiences in the process of R & D platform splitting
- After marriage
- [untitled]
猜你喜欢

Feature query of hypergraph iserver rest Service

【读书笔记】程序员修炼手册—实战式学习最有效(项目驱动)

pytest 测试框架

Jvm-01 (phased learning)

SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding

Actual battle of financial risk control - under Feature Engineering
![[staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)](/img/e0/05890eafdb291c5aaff78cc241f455.jpg)
[staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)
![[staff] pitch representation (bass clef | C1 36 note pitch representation | C2 48 note pitch representation | C3 60 note pitch representation)](/img/98/956d8abbccceb1aae47e25825bc63d.jpg)
[staff] pitch representation (bass clef | C1 36 note pitch representation | C2 48 note pitch representation | C3 60 note pitch representation)

CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes

What is the principle of bone conduction earphones and who is suitable for bone conduction earphones
随机推荐
Query word weight, search word weight calculation
Opencascade7.6 compilation
What is the function of the headphone driver
研发中台拆分过程的一些心得总结
Which kind of sports headphones is easier to use? The most recommended sports headphones
【带你学c带你飞】day 5 第2章 用C语言编写程序(习题2)
Mathematics in Sinorgchem: computational geometry
Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched
Systemserver service and servicemanager service analysis
Actual battle of financial risk control - under Feature Engineering
What are the characteristics of common web proxy IP
trading
[deep learning] Infomap face clustering facecluster
Es interview questions
Sword finger offer 31 Stack push in and pop-up sequence
pytest 测试框架
[JSON] gson use and step on the pit
DNS domain name resolution
使用开源项目【Banner】实现轮播图效果(带小圆点)
LFM signal denoising, time-frequency analysis, filtering