forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp11_test.cc
88 lines (74 loc) · 1.89 KB
/
cpp11_test.cc
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <memory>
#include <unordered_map>
#include <vector>
#include "ortools/base/hash.h"
namespace operations_research {
struct Foo {
Foo() { std::cout << "Foo::Foo\n"; }
~Foo() { std::cout << "Foo::~Foo\n"; }
void bar() { std::cout << "Foo::bar\n"; }
};
void f(const Foo& foo) { std::cout << "f(const Foo&)\n"; }
void test_unique() {
std::cout << "test_unique" << std::endl;
std::unique_ptr<Foo> p1(new Foo); // p1 owns Foo
if (p1) p1->bar();
{
std::unique_ptr<Foo> p2(std::move(p1)); // now p2 owns Foo
f(*p2);
p1 = std::move(p2); // ownership returns to p1
std::cout << "destroying p2...\n";
}
if (p1) p1->bar();
// Foo instance is destroyed when p1 goes out of scope
}
void test_auto() {
std::cout << "test_auto" << std::endl;
std::vector<int> numbers;
numbers.push_back(1);
numbers.push_back(2);
numbers.push_back(3);
numbers.push_back(4);
numbers.push_back(5);
numbers.push_back(6);
numbers.push_back(7);
for (int vec : numbers) {
std::cout << vec << std::endl;
}
std::unordered_map<std::string, int> my_map;
my_map["toto"] = 2;
for (auto mm : my_map) {
std::cout << mm.first << " -> " << mm.second << std::endl;
}
}
void test_chevron() {
std::cout << "test_chevron" << std::endl;
std::vector<std::pair<int, int>> toto;
toto.push_back(std::make_pair(2, 4));
}
class A {
public:
virtual ~A() {}
virtual int V() const { return 1; }
};
class B : public A {
public:
~B() override {}
int V() const override { return 2; }
};
void test_override() {
std::cout << "test_override" << std::endl;
B* b = new B();
if (b->V() != 2) {
std::cout << "Problem with override" << std::endl;
}
}
} // namespace operations_research
int main() {
operations_research::test_unique();
operations_research::test_auto();
operations_research::test_chevron();
operations_research::test_override();
return 0;
}