forked from changkun/modern-cpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.9.small.features.cpp
More file actions
43 lines (36 loc) 路 1.15 KB
/
Copy path11.9.small.features.cpp
File metadata and controls
43 lines (36 loc) 路 1.15 KB
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
//
// 11.9.small.features.cpp
// chapter 11 cpp23
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
#include <bit>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
// A function object whose call operator is static (C++23): no implicit
// object parameter, so it can be called without an instance overhead.
struct Add {
static int operator()(int a, int b) { return a + b; }
};
int main() {
// auto(x): an explicit decay-copy that produces a prvalue copy.
std::vector<int> v{1, 2, 3};
auto copy = auto(v);
std::cout << "copy size: " << copy.size() << '\n';
// static operator()
std::cout << "Add: " << Add{}(2, 3) << '\n';
// std::string::contains (C++23)
std::string s = "modern c++";
std::cout << "contains: " << s.contains("c++") << '\n';
// std::byteswap (C++23)
std::cout << std::hex << "byteswap: "
<< std::byteswap(std::uint16_t{0x1234}) << std::dec << '\n';
// [[assume]] gives the optimizer a precondition it may rely on.
int x = 10;
[[assume(x > 0)]];
std::cout << "x = " << x << '\n';
}