https://oopscenities.net/2012/08/09/reference_wrapper/

Look at this piece of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <functional>
 
using namespace std;
using namespace std::placeholders;
 
void add(int a, int b, int& r)
{
    r = a + b;
}
 
int main()
{
    int result = 0;
     
    auto f = bind(add, _1, 20, result);
     
    f(80);
     
    cout << result << endl;
    return 0;
}

This program supposedly adds 80 to 20 and prints the result; it compiles perfectly but when you execute it; you get…. 0!

Why?

Because the bind method receives its parameters as parameters-by-value and the “result” variable is copied before being passed to the bound function add. Why?
Because bind does not know if the parameters will still be valid when the actual invocation will be performed (remember, you could pass a function object to other function passing local variables as arguments and invoking it from there).

The solution? Pretty simple:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    int result = 0;
     
    auto f = bind(add, _1, 20, ref(result));
     
    f(80);
     
    cout << result << endl;
    return 0;
}

I added the function ref that sends our parameter as a reference to the bound function.

What does this function ref do?

It is a template function that returns a reference_wrapper object. A reference_wrapper is a class template that wraps a reference in a concrete object.

Actually you could do something like:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    int result = 0;
     
    reference_wrapper<int> result_ref(result);
    auto f = bind(add, _1, 20, result_ref);
     
    f(80);
     
    cout << result << endl;
    return 0;
}

and everything would continue working as expected.

As you can see, you can pass the reference_wrapper by value and everything will work because its copy constructor copies the reference (actually, the reference_wrapper implementations do not store a reference but a pointer to the data being referenced, but their methods expose it as a reference).

Other nice usage of this would be in cases where you need to have a container of references (the actual objects are stored in other container or in other place and you do not need/want to have copies or pointers to them). For example, you have these classes:

1
2
class A { };
class B : public A { };

And you want to have at the same time local variables pointing to them and you want them stored in a container:

1
2
3
4
5
6
7
int main()
{
  A a, c;
  B b, d;
 
  vector<A> v = { a, b, c, d };
}

Good? No! Bad at all! You are storing instances of class A in your vector. All the instances of B will be copied as instances of A (losing their specific attributes and all the polymorphic behavior and so on).

One solution? Storing pointers:

1
2
3
4
5
6
7
int main()
{
  A a, c;
  B b, d;
 
  vector<A*> v = { &a, &b, &c, &d };
}

It works, but it is not evident for the user of the container if s/he will be in charge of freeing the objects or not.

Other solution? Using references:

1
2
3
4
5
6
7
int main()
{
  A a, c;
  B b, d;
 
  vector<A&> v = { a, b, c, d };
}

Looks nice, but it does not compile; because you cannot specify reference types in a vector.

Real solution: Using reference_wrappers:

1
2
3
4
5
6
7
int main()
{
  A a, c;
  B b, d;
 
  vector<reference_wrapper<A>> v = { a, b, c, d };
}

Someone could argue: In which scenario is this thing useful?

If you create a UI frame using Java Swing, you probably create a subclass of the JFrame class, will specify your visual components as member variables and you will also add them into the JFrame’s component list. Implementing something similar in C++ using reference_wrappers would be quite elegant.

C++11: reference_wrapper的更多相关文章

  1. 地区sql

    /*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...

  2. C++11的一些新特性

    3.1.9崭新的Template特性 Variadic Template 可变参数模板 void print() { } template <typename T, typename… Type ...

  3. C++11包装引用

    [C++11包装引用] 我们可以通过实体化样板类 reference_wrapper 得到一个包装引用 (wrapper reference).包装引用类似于一般的引用.对于任意对象,我们可以通过模板 ...

  4. c++11 : range-based for loop

    0. 形式 for ( declaration : expression ) statement 0.1 根据标准将会扩展成这样的形式: 1   { 2     auto&& __ra ...

  5. [转载] C++11新特性

    C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...

  6. C++11新特性之八——函数对象function

    详细请看<C++ Primer plus>(第六版中文版) http://www.cnblogs.com/lvpengms/archive/2011/02/21/1960078.html ...

  7. 引用传参与reference_wrapper

    本文是<functional>系列的第3篇. 引用传参 我有一个函数: void modify(int& i) { ++i; } 因为参数类型是int&,所以函数能够修改传 ...

  8. C++11<functional>深度剖析

    自C++11以来,C++标准每3年修订一次.C++14/17都可以说是更完整的C++11:即将到来的C++20也已经特性完整了. C++11已经有好几年了,它的年龄比我接触C++的时间要长10倍不止吧 ...

  9. WinForm 天猫2013双11自动抢红包【源码下载】

    1. 正确获取红包流程 2. 软件介绍 2.1 效果图: 2.2 功能介绍 2.2.1 账号登录 页面开始时,会载入这个网站:https://login.taobao.com/member/login ...

随机推荐

  1. 分布式框架Dubbo配置和实例

    准备工作: 1.ZooKeeper:需要去Apache Zookeeper官网下载Zookeeper.tar.gz包,Dubbo是依赖于Zookeeper的 2.Maven:需要去Apache Mav ...

  2. FZU - 1492(Problem 1492 地震预测)

    怀特先生是一名研究地震的科学家,最近他发现如果知道某一段时间内的地壳震动能量采样的最小波动值之和,可以有效地预测大地震的发生. 假设已知一段时间的n次地壳震动能量的采样值为a1,a2,-an,那么第i ...

  3. codevs1033 蚯蚓的游戏问题 裸最小费用最大流,注意要拆点

    因为蚯蚓走过的路径不能重合,所以把每个点拆成两个点,容量赋为1,保证不会走过相同的点,再加超级源点(程序中为1)和一个辅助点(程序中为2)容量赋为k来控制蚯蚓的数量,最后汇集到一个超级汇点上.做一遍最 ...

  4. 洛谷P2782 友好城市

    题目描述 有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的N个城市.北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同.没对友好城市都向政府申请在河上开辟一条直线航 ...

  5. Android Studio初级介绍

    Android Studio原来不咋地,但是现在可以尝试抛弃eclipse转用它了, 亲儿子到底是亲儿子,现在的Android Studio已经今非昔比,用了一段时间,简直爱不释手,我觉得,It's ...

  6. [NOIp2016提高组]愤怒的小鸟

    题目大意: 平面直角坐标系的第一象限有n(n<=18)个点,你可以每次给出一个形如y=ax^2+bx的函数把经过这条函数的点消掉,问消掉所有的点至少要多少函数? 思路: 枚举每两个点对,可以唯一 ...

  7. mybatis批量update,返回行数为-1

    mybatis批量更新返回结果为-1,是由于mybatis的defaultExExecutorType引起的,    它有三个执行器:SIMPLE 就是普通的执行器:REUSE 执行器会重用预处理语句 ...

  8. Highcharts做统计图。

    <script> $(function () { var areasplineData = [[1447916401000,3],[1447918201000,4]]; var typeR ...

  9. [51nod1357]密码锁 暨 GDOI2018d1t2

    有一个密码锁,其有N位,每一位可以是一个0~9的数字,开启密码锁需要将锁上每一位数字转到解锁密码一致.这个类似你旅行用的行李箱上的密码锁,密码锁的每一位其实是一个圆形转盘,上面依次标了0,1,...9 ...

  10. iOS中 xib自定义View在storyboard中的使用

    1,创建UIView 的SubClass 命名为MyView 2, new一个名为MyView的xib p1 3,配置xib的属性 p2 4,为View 添加背景色,添加一个按钮并定制按钮约束,这里我 ...