Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
C++11
1. Jerusalem .NET/C++ User Group
• Bi-monthly meetings
– Alternating C++ and .NET content
– May have some mixed content as well
• Please participate!
• Sponsors: BrightSource, SELA Group
2. // C++ 11
/*
Sasha Goldshtein
CTO, SELA Group
blog.sashag.net | @goldshtn
*/
3. // The New C++
/*
* ISO Standard as of September ‘11
* Completely new C++ style
* Dozens of language features
* Dozens of STL features
* GCC has full support (exp.)
* VS10 – partial, VS11 – more
*/
4. // Automatic Type Inference
auto x = 17;
std::map<std::string, std::list<int>> m;
auto it = m.begin(); //also: cbegin(), begin(m)
auto f = std::bind(std::less<float>, _1, 3.0f);
int arr[100];
const auto& s = &arr[42];
5. // Range-Based For Loop
std::vector<int> v;
for (int n : v) {
std::cout << n << std::endl;
}
std::map<int, std::list<std::string>> m;
for (auto& p : m) {
p.second.push_back(“Winter is coming”);
}
8. // Higher-Order Functions
auto id = [](int n) { return n; };
auto inc = [](const std::function<int(int)> f)
{
return [](int n) { return f(n) + 1; }
};
auto f = id;
f = inc(f); std::cout << f(1);
f = inc(f); std::cout << f(1);
9. // Lambda Capture
void foo() {
int x = 42;
auto f = [x]() { std::cout << x; };
f();
auto g = [&x]() { ++x; };
g();
std::cout << x;
auto h = [x]() mutable { ++x; };
h();
std::cout << x;
}
10. // Rvalue References
int x;
x = 42; //OK, x is an lvalue
int f();
f() = 42; //NOT OK, f() is an rvalue
int& g();
g() = 42; //OK, g() is an lvalue
/* An rvalue reference is a reference
to an rvalue, written as && ...
WHY?! */
11. // Rvalue References
template <typename T>
void swap(T& a, T& b) {
T temp(a); a = b; b = temp;
} //THREE deep copies are made!
template <typename T>
void swap(T& a, T& b) {
T temp(a); //Assuming T has move ctor
a = std::move(b);
b = std::move(temp);
} //ZERO deep copies are made!
12. // Rvalue References
class sstring {
char* data; int len;
public:
sstring(const string& other) :
data(strdup(other.data), len(other.len) {}
sstring& operator=(const string& other)
if (this == &other) return *this;
free(data); data = strdup(other.data);
len = other.len;
}
};
13. // Rvalue References
std::vector<sstring> v;
for (int k = 0; k < 100; ++k) {
v.push_back(sstring(“Hello”));
}
// How many times “Hello” is allocated?
// Answer: 200
// How many times “Hello” is freed?
// Answer: 100
14. // Rvalue References
class sstring { //Continued
public:
sstring(sstring&& other) {
data = other.data; other.data = nullptr;
len = other.len;
}
sstring& operator=(sstring&& other) {
//similar
}
};
15. // Rvalue References
std::vector<sstring> v;
for (int k = 0; k < 100; ++k) {
v.push_back(sstring(“Hello”));
}
// How many times “Hello” is allocated?
// Answer: 100
// How many times “Hello” is freed?
// Answer: 0
16. // Smart Pointers
/*
* Three smart pointer classes
* No more new and delete
* unique_ptr<T> - single ownership
* shared_ptr<T> - shared ownership
* weak_ptr<T> - cycle breaking
*/
17. // Threads and Async
std::thread t([]() { /*body*/ });
void quicksort(int* a, int l, int r) {
int pivot = partition(a, l, r);
auto fl = std::async([&]() {
quicksort(a, l, pivot);
});
auto f2 = std::async([&]() {
quicksort(a, pivot+1, r);
});
f1.wait(); f2.wait();
}
19. // Summary: C++ Style
/*
* Rely heavily on smart pointers
* Use lambdas when necessary
* Implement move semantics
*
* We only scratched the surface!
*/