Files
ortools-clone/examples/tests/cpp11_test.cc

75 lines
1.6 KiB
C++
Raw Normal View History

2012-11-13 01:20:50 +00:00
#include <iostream>
#include <memory>
#include <vector>
2012-11-13 06:55:55 +00:00
#include "base/hash.h"
namespace operations_research {
#if defined(__linux__) || defined(_MSC_VER) || defined(__APPLE__)
2012-11-13 01:25:09 +00:00
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";
}
2012-11-13 01:20:50 +00:00
2012-11-13 01:25:09 +00:00
void test_unique() {
2012-11-13 16:06:51 +00:00
std::cout << "test_unique" << std::endl;
2012-11-13 01:25:09 +00:00
std::unique_ptr<Foo> p1(new Foo); // p1 owns Foo
if (p1) p1->bar();
2012-11-13 01:20:50 +00:00
2012-11-13 01:25:09 +00:00
{
std::unique_ptr<Foo> p2(std::move(p1)); // now p2 owns Foo
f(*p2);
2012-11-13 01:20:50 +00:00
2012-11-13 01:25:09 +00:00
p1 = std::move(p2); // ownership returns to p1
std::cout << "destroying p2...\n";
}
2012-11-13 01:20:50 +00:00
2012-11-13 01:25:09 +00:00
if (p1) p1->bar();
// Foo instance is destroyed when p1 goes out of scope
}
2012-11-13 16:06:51 +00:00
#else
void test_unique() {
std::cout << "test_unique not launched" << std::endl;
}
2012-11-13 01:25:09 +00:00
#endif
2012-11-13 01:20:50 +00:00
void test_auto() {
2012-11-13 16:06:51 +00:00
std::cout << "test_auto" << std::endl;
2012-11-13 01:20:50 +00:00
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);
2013-04-18 13:59:34 +00:00
for (int vec : numbers) {
2012-11-13 06:55:55 +00:00
std::cout << vec << std::endl;
}
hash_map<std::string, int> my_map;
my_map["toto"] = 2;
for (auto mm : my_map) {
std::cout << mm.first << " -> " << mm.second << std::endl;
2012-11-13 01:20:50 +00:00
}
}
2012-11-13 06:55:55 +00:00
void test_chevron() {
2012-11-13 16:06:51 +00:00
std::cout << "test_chevron" << std::endl;
2012-11-13 06:55:55 +00:00
std::vector<std::pair<int,int>> toto;
toto.push_back(std::make_pair(2, 4));
}
} // namespace operations_research
2012-11-13 01:20:50 +00:00
int main() {
2012-11-13 06:55:55 +00:00
operations_research::test_unique();
operations_research::test_auto();
operations_research::test_chevron();
2012-11-13 01:20:50 +00:00
return 0;
}