File: reference_wrapper.cpp

package info (click to toggle)
yrmcds 1.0.4-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 924 kB
  • ctags: 1,346
  • sloc: cpp: 9,634; sh: 133; makefile: 97
file content (31 lines) | stat: -rw-r--r-- 663 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <functional>
#include <iostream>
#include <vector>

struct foo {
    int i;
};

int main() {
    int *p = nullptr;
    std::reference_wrapper<int> a[3] = {*p, *p, *p};
    int i = 3;
    a[0] = std::ref(i);
    if( &(a[2].get()) == nullptr ) {
        std::cout << "null!" << std::endl;
    } else {
        std::cout << "wtf" << std::endl;
    }
    a[0].get() = 5;
    std::cout << "i = " << i << std::endl;

    foo f;
    f.i = 3;

    std::vector<std::reference_wrapper<const foo>> v;
    v.emplace_back(f);
    std::cout << "f.i = " << v[0].get().i << std::endl;
    f.i = 5;
    std::cout << "f.i = " << v[0].get().i << std::endl;
    return 0;
}