Prezi

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in the manual

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

KCDC '13 - Modern C++: Library Features

No description
by Michael Price on 4 May 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of KCDC '13 - Modern C++: Library Features

Standard Library Features Modern C++ Language Integration move constructors
and operators are
pervasive typedef vector<map<string, string>> Dictionaries;

// Before move construction...
// returning vectors was EXPENSIVE!!!
//
void getDictionaries (Dictionaries & d)
{
/* populate d */
return;
}

// But now, we can use the natural
// syntax with minimal performance impact
//
Dictionaries getDictionaries ()
{
Dictionaires dictionaries;
/* populate dictionaries */
return dictionaries;
} functions with predicates or functors can also use lambdas, in fact lambdas are preferred in most cases void gotta_sort_em_all ()
{
vector<string> pokemon = { "Pikachu",
"Charmander",
"Squirtle" };

auto srt = [] (const string & l,
const string & r)
{
return (l.length() < r.length());
};

sort(begin(pokemon), end(pokemon), srt);
} uniform initialization & initializer lists void i_wasted_all_the_bits_in_this_name ()
{
int numArray[] = {3, 1, 4, 1, 5};

// Good thing I don't have to use
// push_back()!
//
vector<char> vChars = {'A', 'D', 'G',
'J', 'M', 'Q', 'U', 'Z'};

for (auto s : {"Hello", "World"})
{
cout << s << endl;
}

} struct RememberTheInitialSize
{
RememberTheInitialSize (std::initializer_list<int> il)
: initialSize(il.size())
{ }

size_t initialSize = 0;
};

void what_was_that_size_again ()
{
RememberTheInitialSize please = {1, 6, 1, 8, 0, 3};
std::cout << please.initialSize << std::endl;
} variadic templates (examples later) many things are constexpr and more are on the way void holding_constant ()
{
constexpr size_t small = numeric_limits<int>::min();


std::array<int, 5> a = {1, 9, 8, 1, 7};
constexpr size_t asize = a.size();
} Containers std::array is a safe alternative to C arrays void safety_arrays ()
{
char builtin[] = {'H','e','y'};
array<char,3> arr = {'Y','o','u'};

// Ooops! out-of-bounds
cout << builtin[3] << endl;

// Throws out_of_range exception
cout << arr.at(3) << endl;

// Or you can opt to shoot at your foot
cout << arr[3] << endl;

} noexcept is used throughout Concurrency Miscellaneous use std::begin and std::end instead of member methods template <typename TyContainer>
void alphaAndOmega (TyContainer && c)
{
// begin and end work with builtin arrays as well
// as standard containers
//
for (auto e = begin(c); e != end(c), ++e)
{
cout << e << endl;
}

// begin and end are also the basis for range-for
} void holding_constant ()
{
constexpr size_t small = numeric_limits<int>::min();


std::array<int, 5> a = {1, 9, 8, 1, 7};
constexpr size_t asize = a.size();
} std::tuple for 'on-the-fly' simple type containers tuple<int, double, string> plural ()
{
auto t = make_tuple(1, 2.0, "Hello");

cout << get<0>(t) << endl;
cout << get<1>(t) << endl;
cout << get<2>(t) << endl;

// Compilation error!
cout << get<3>(t) << endl;
} forward_list unordered_map unordered_set unordered_multimap unordered_multiset emplacement insertion scoped allocators mutex future sleep_for yield condition_variable sleep_until promise timed_mutex recursive_mutex timed_recursive_mutex call_once lock try_lock and several others <type_traits> <chrono> <algorithm> additions current_exception rethrow_exception copy_exception garbage collection helpers quick_exit at_quick_exit C++14 std::thread...
it's about time! std::atomic for fine-grained locking std::async() for task-oriented programming std::unique_ptr replaces 'deficient' std::auto_ptr use std::shared_ptr and std::weak_ptr for shared ownership std::function & std::bind random number generation regular expressions void no_io_from_current_thread ()
{
std::array<int, 3> arr = {1, 2, 3};

std::thread do_io = std::thread([&arr]()
{
for (auto e : arr)
std::cout << e << std::endl;
});

do_io.join();
} TODO void even_or_odd ()
{
std::array<int, 10> jumble = {9,6,3,1,7,3,8,0,4,2};

auto is_even = [](int e) { return e%2 == 0; };
auto is_odd = [](int e) { return e%2 != 0; };

auto count_of_evens = std::async([=]()
{
return std::count_if(std::begin(jumble),
std::end(jumble), is_even);
});

auto count_of_odds = std::async([=]()
{
return std::count_if(std::begin(jumble),
std::end(jumble), is_odd);
});

std::cout << "Evens: " << count_of_evens.get()
<< " Odds: " << count_of_odds.get()
<< std::endl;
} void someone_anyone ()
{
std::atomic<int> more {0};

auto one = std::thread([&more]()
{ more++; });

auto two = std::thread([&more]()
{ more++; });

one.join();
two.join();

std::cout << more << std::endl;
} void pointer_zone ()
{
auto_ptr<int> ap{new int{5}};
vector<auto_ptr<int>> vap;
vap.push_back(ap); // NO! Error!

unique_ptr<int> up{new int{5}};
vector<unique_ptr<int>> uap;
uap.push_back(move(up));
// 'up' should be empty now

// avoid using delete, use smart
// pointers instead. raw pointers
// should indicate 'an optional value
// that I do not own'
} void functional ()
{
typedef int(*OldFn)(int);
typedef std::function<int (int)> NewFn;

OldFn works = [](int) { return 1; };
NewFn works_too = [](int) { return 2; };

int stuff = 3;
//OldFn error = [stuff](int) { return stuff; };
NewFn not_an_error = [stuff](int) { return stuff; };

auto tester = [](int i, std::function<bool (int)> test)
{ std::cout << std::boolalpha << test(i) << std::endl; };

tester(2, [](int i) { return i%2 != 0; });

auto is_odd = std::bind(tester, std::placeholders::_1,
[](int i) { return i%2 != 0; });
is_odd(3);
} class LifeMonitor
{
public:
LifeMonitor (const std::weak_ptr<int> & wp) : m_object(wp)
{ }

void status ()
{
std::cout << std::boolalpha
<< (m_object.lock().get() != nullptr) << std::endl;
}

private:
std::weak_ptr<int> m_object;
};

void monitoring ()
{
auto sp = std::make_shared<int>(5);

LifeMonitor lm(sp); lm.status();
sp.reset(); lm.status();
sp.reset(new int(10)); lm.status();
} template <typename E, typename D>
void print_dist (E && eng, D && dist)
{
std::vector<int> vec(16);

for (size_t i = 0; i<128; ++i)
++vec[static_cast<size_t>(dist(eng))];

for (size_t i = 0; i < vec.size(); ++i)
{
std::cout << i << '\t' << std::string(vec[i], '*')
<< std::endl;
}
}

void random_i_nator ()
{
// Engine & Distribution
std::mt19937 engine;

print_dist(engine, std::geometric_distribution<>(0.5));
print_dist(engine, std::normal_distribution<>(8, 1));
print_dist(engine, std::uniform_int_distribution<>(4, 11));
} void regulary_scheduled_maintenance ()
{
regex broken_numerals(R"((\d+)\D+(\d+))");
cout << boolalpha;

cout << regex_match("", broken_numerals) << endl;
cout << regex_match("1234", broken_numerals) << endl;
cout << regex_match("12x34", broken_numerals) << endl;
cout << regex_match("a12x34", broken_numerals) << endl;

cout << regex_search("a12x34", broken_numerals) << endl;

string fixed = regex_replace("a12x34b56c78d",
broken_numerals,
"$1$2");
cout << fixed << endl;
} Normal Distribution Geometric Distribution Uniform Distribution 0 **********************************************************************
1 ****************************
2 ****************
3 *****
4 ******
5 *
6
7 *
8 *
9
10
11
12
13
14
15 0
1
2
3
4
5 *
6 ****************
7 *********************************************
8 *************************************
9 *********************
10 ********
11
12
13
14
15 0
1
2
3
4 **************
5 *********************
6 *************
7 *********************
8 ******************
9 *****************
10 ***********
11 *************
12
13
14
15 https://github.com/michaelbprice/modern_cpp Networking Filesystem make_unique dynarray optional tuple access
by-type chrono UDLs http://isocpp.org SILVER SPONSORS GOLD SPONSORS PLATINUM SPONSORS
See the full transcript