see #12: translate ch09

This commit is contained in:
Changkun Ou
2019-07-14 23:10:07 +02:00
parent 3fc59f9bea
commit 9779283735
6 changed files with 189 additions and 24 deletions

View File

@@ -1,7 +0,0 @@
#include <list>
#include <algorithm>
int main() {
std::list<int> l = {1, 2, 3};
std::sort(l.begin(), l.end());
return 0;
}

50
code/9/9.1.noexcept.cpp Normal file
View File

@@ -0,0 +1,50 @@
//
// 8.1.cpp
// modern c++ tutorial
//
// created by changkun at changkun.de
//
// noexcept
#include <iostream>
void may_throw() {
throw true;
}
auto non_block_throw = []{
may_throw();
};
void no_throw() noexcept {
return;
}
auto block_throw = []() noexcept {
no_throw();
};
int main()
{
std::cout << std::boolalpha
<< "may_throw() noexcept? " << noexcept(may_throw()) << std::endl
<< "no_throw() noexcept? " << noexcept(no_throw()) << std::endl
<< "lmay_throw() noexcept? " << noexcept(non_block_throw()) << std::endl
<< "lno_throw() noexcept? " << noexcept(block_throw()) << std::endl;
try {
may_throw();
} catch (...) {
std::cout << "exception captured from my_throw()" << std::endl;
}
try {
non_block_throw();
} catch (...) {
std::cout << "exception captured from non_block_throw()" << std::endl;
}
try {
block_throw();
} catch (...) {
std::cout << "exception captured from block_throw()" << std::endl;
}
}

33
code/9/9.2.literals.cpp Normal file
View File

@@ -0,0 +1,33 @@
//
// 8.2.cpp
// modern c++ tutorial
//
// created by changkun at changkun.de
//
// literals
#include <iostream>
#include <string>
std::string operator"" _wow1(const char *wow1, size_t len) {
return std::string(wow1)+"woooooooooow, amazing";
}
std::string operator""_wow2 (unsigned long long i) {
return std::to_string(i)+"woooooooooow, amazing";
}
int main() {
std::string str = R"(C:\\File\\To\\Path)";
std::cout << str << std::endl;
int value = 0b1001010101010;
std::cout << value << std::endl;
auto str2 = "abc"_wow1;
auto num = 1_wow2;
std::cout << str2 << std::endl;
std::cout << num << std::endl;
return 0;
}

7
code/9/Makefile Normal file
View File

@@ -0,0 +1,7 @@
all: $(patsubst %.cpp, %.out, $(wildcard *.cpp))
%.out: %.cpp Makefile
clang++ $< -o $@ -std=c++2a -pedantic
clean:
rm *.out