Contenu connexe Similaire à C++17 not your father’s c++ (20) Plus de Patrick Viafore (12) C++17 not your father’s c++6. hello.c
// my first c program
#include <stdio.h>
int main()
{
printf("Hello, %sn", "World!");
return 0;
}
7. hello.cpp
// my first c++ program
#include <stdio.h>
int main()
{
printf("Hello, %sn", "World!");
return 0;
}
8. Is this all C++ is?
● std::cout?
● std::vector?
● std::map?
● std::string?
● classes w/ inheritance?
12. 4 subsets of C++
1. The C programming language
2. Object-Oriented
3. STL library
4. Template Metaprogramming
16. We’re going to look at
modern C++ and see how
it has transformed from
“C With Classes”
33. C++17 Destructuring C++ 17
//c++11
int i;
float f;
std::string s;
std::tie(i,f,s) = make_tuple(5, 3.4, “c++11”);
35. C++17 Attributes C++ 17
switch(condition) {
case 1:
std::cout <<”Option 1n”;
break;
case 3:
std::cout <<”Exceptional Valuen”;
[[fallthrough]];
default:
break;
}
37. C++17 Attributes C++ 17
[[nodiscard]] int importantFunction();
[[nodiscard]] struct lockGuard {};
importantFunction(); //warning
lockGuard f();
f(); //warning
39. C++17 Miscellany C++ 17
namespace x::y::z {
void print() {
std::byte b = std::byte(0b1001);
b = clamp(b, std::byte(5), std::byte(8));
if(auto opt = getOpts(); opt.isConsole) {
int i = std::to_integer<int>(b);
std::cout << i << "n";
}
}
}
44. Reproduction of Joe Armstrong’s
picture by Yossi Kreinin
http://yosefk.com/blog/parallelism-and-concurrency-nee
d-different-tools.html
45. Parallelism
std::vector<int> v = {1,2,3,4,5};
auto times3 = [](int i) { return i * 3;};
std::transform(std::par, v.begin(), v.end(),
v.begin(), times3);
C++ 17
56. Type Inference (continued)
auto isSpecial = [](int x) { return x == 5; };
auto a = [](auto x, auto y) { return x + y; };
auto get_second(auto &container) {
return container.begin() + 1;
}
C++ 11/14
62. A Quick Look at Haskell
data Custom = Option1 | Option2 Int
doSomething :: Custom -> Int
doSomething Option1 = 0
doSomething (Option2 x) = x
63. std::visit
template<class... Ts> struct make_visitor : Ts...
{ using Ts::operator()...; };
template<class... Ts> make_visitor(Ts...) ->
make_visitor<Ts...>;
std::variant<int, float, std::string> v;
C++17
66. std::any
auto a = std::any(12);
std::cout << std::any_cast<int>(a) << “n”;
//throws bad_any_cast exception
std::any_cast<std::string>(a)
C++17
75. constexpr
constexpr unsigned int factorial(int n) {
return n <= 0 ? 1 : (n * factorial(n - 1));
}
int main() {
std::cout << getBits<factorial(5)>();
std::cout << factorial(5);
return 0;
}
C++ 11
76. If constexpr
template <typename T>
void printSpecialized(T t){
if constexpr (is_integral<T>())
std::cout << "Integral " << t << "n";
else if constexpr(is_floating_point<T>())
std::cout << "Float " << t << "n";
}
C++ 17
80. Ranges
std::vector numbers = { 1, 2, 3, 4, 5 };
ranges::accumulate(numbers |
view::transform(addOne) |
view::filter(isEven) |
view::transform(invert)
, 0);
C++ ?
84. Here’s my personal litmus test for whether
we’ve changed the way people program: Just
as you can take a look at a screenful of code
and tell that it’s C++11 (not C++98), if you
can look at a screenful of code and tell that
it’s C++17, then we’ve changed the way we
program C++. I think C++17 will meet that
bar.
--Herb Sutter
85. There are only two kinds of languages: the
ones people complain about and the ones
nobody uses.
--Bjarne Stroustrup