├── README.txt ├── les02_distance.cpp ├── les02_palindrome.cpp ├── les02_palindrome_impl01.cpp ├── les02_palindrome_impl02.cpp ├── les02_palindrome_ineff.cpp ├── les03_slides.pdf ├── les04_add_pointer.cpp ├── les04_facilities.cpp ├── les04_factorial.cpp ├── les04_key.cpp ├── les04_partial_specialization.cpp ├── les04_recursive_remove_pointer.cpp ├── les04_remove_pointer.cpp ├── les04_type.cpp ├── les05_SFINAE_enable_if.cpp ├── les05_SFINAE_negate.cpp ├── les05_palindrome_using_iterator_tag.cpp ├── les06_1.cpp ├── les06_2_my_count_if.cpp ├── les06_3_string.cpp ├── les06_4_lambda.cpp ├── les06_4_lambda_int.cpp ├── les06_5_accumulate.cpp ├── les07_1_transform.cpp ├── les07_2_bind.cpp ├── les07_3_bind.cpp ├── les07_4_secret.cpp ├── les07_5_secret_std_function.cpp ├── les08_1_call_by_value.cpp ├── les08_2_call_by_reference.cpp ├── les08_3_vector_by_const_ref.cpp ├── les08_4_move_semantics.cpp ├── les08_5_move_semantics_rvalue.cpp └── les10_slides_threading.pdf /README.txt: -------------------------------------------------------------------------------- 1 | C++ Summer Lecture Series 2016 2 | 3 | During the Summer of 2016 the STE||AR Group @ LSU will be hosting 4 | a C++ lecture series. These sessions will cover advanced C++ techniques 5 | which are widely used in HPX libraries and applications such as the 6 | STL, SFINAE, and meta-template programming to name a few. 7 | 8 | You can find a copy of the code presented in these talks here in this 9 | repository, which complement the videos posted on our YouTube Channel: 10 | https://www.youtube.com/channel/UCymrreFOGmolvjYCL67Sx5w 11 | -------------------------------------------------------------------------------- /les02_distance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | template 11 | struct coordinate 12 | { 13 | T x, y; 14 | }; 15 | 16 | template 17 | T sqr(T val) { return val * val; } 18 | 19 | template 20 | decltype(auto) distance(coordinate lhs, coordinate rhs) 21 | //-> decltype(std::sqrt(sqr(lhs.x - rhs.x) + sqr(lhs.y - rhs.y))) 22 | { 23 | return std::sqrt(sqr(lhs.x - rhs.x) + sqr(lhs.y - rhs.y)); 24 | } 25 | 26 | int main() 27 | { 28 | 29 | coordinate c = { 1.0, 2.0 }; 30 | coordinate f { 1.5, 2.5 }; 31 | 32 | std::cout << "type = " << typeid(distance(c, f)).name() << std::endl ; 33 | std::cout << "distance = " << distance(c, f) << std::endl ; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /les02_palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | bool is_palindrome(std::string s) 8 | { 9 | auto middle = s.begin(); 10 | std::advance( middle, s.size() / 2 ); 11 | return std::equal( s.begin(), middle, s.rbegin() ); 12 | } 13 | 14 | int main() 15 | { 16 | std::cout << "madam is a palindrome " << is_palindrome("madam") << std::endl; 17 | std::cout << "test is a palindrome " << is_palindrome("test") << std::endl; 18 | std::cout << "racecar is a palindrome " << is_palindrome("racecar") << std::endl; 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /les02_palindrome_impl01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | template 9 | void my_advance(Iter& it, int n) 10 | { 11 | while(n--) 12 | ++it; 13 | } 14 | 15 | template 16 | 17 | bool is_palindrome(Container s) 18 | { 19 | auto middle = s.begin(); 20 | //std::advance( middle, s.size() / 2 ); 21 | my_advance( middle, s.size() / 2 ); 22 | return std::equal( s.begin(), middle, s.rbegin() ); 23 | } 24 | 25 | int main() 26 | { 27 | std::vector l = {'m', 'a', 'd', 'a', 'm'}; 28 | for(auto& i : l) std::cout << i ; // print the vector as a word 29 | std::cout << " is a palindrome: " << is_palindrome(l) << std::endl; 30 | 31 | std::vector t = {'t', 'e', 's', 't'}; 32 | for(auto& i : t) std::cout << i ; // print the vector as a word 33 | std::cout << " is a palindrome: " << is_palindrome(t) << std::endl; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /les02_palindrome_impl02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | template 9 | void my_advance(Iter& it, int n) 10 | { 11 | while(n--) 12 | ++it; 13 | } 14 | 15 | template 16 | void my_advance(Iter& it, int n) 17 | { 18 | my_advance(it, n, is_random_access_iterator_t()); 19 | } 20 | 21 | template 22 | bool is_palindrome(Container s) 23 | { 24 | auto middle = s.begin(); 25 | //std::advance( middle, s.size() / 2 ); 26 | my_advance( middle, s.size() / 2 ); 27 | return std::equal( s.begin(), middle, s.rbegin() ); 28 | } 29 | 30 | int main() 31 | { 32 | std::vector l = {'m', 'a', 'd', 'a', 'm'}; 33 | for(auto& i : l) std::cout << i ; // print the vector as a word 34 | std::cout << " is a palindrome: " << is_palindrome(l) << std::endl; 35 | 36 | std::vector t = {'t', 'e', 's', 't'}; 37 | for(auto& i : t) std::cout << i ; // print the vector as a word 38 | std::cout << " is a palindrome: " << is_palindrome(t) << std::endl; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /les02_palindrome_ineff.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | bool is_palindrome(std::string s) 8 | { 9 | return std::equal( s.begin(), s.end(), s.rbegin() ); 10 | } 11 | 12 | int main() 13 | { 14 | std::cout << "madam is a palindrome " << is_palindrome("madam") << std::endl; 15 | std::cout << "test is a palindrome " << is_palindrome("test") << std::endl; 16 | std::cout << "racecar is a palindrome " << is_palindrome("racecar") << std::endl; 17 | return 0; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /les03_slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STEllAR-GROUP/cpp_lectures_2016/e7332fb8c4df5f202abdcb4756e367de125a69f9/les03_slides.pdf -------------------------------------------------------------------------------- /les04_add_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | struct add_pointer 6 | { 7 | typedef T* type; 8 | }; 9 | 10 | int main() 11 | { 12 | std::cout << typeid(add_pointer::type).name() << std::endl; 13 | std::cout << typeid(add_pointer::type>::type).name() << std::endl; 14 | add_pointer::type foo= 0; 15 | 16 | return 0; 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /les04_facilities.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | // these types are in std:: struct true_type 6 | struct my_true_type 7 | { 8 | static bool const value = true; 9 | typedef my_true_type type; 10 | }; 11 | 12 | struct my_false_type 13 | { 14 | static bool const value = false; 15 | typedef my_false_type type; 16 | }; 17 | 18 | //main template will be used if T is not a pointer 19 | template 20 | struct is_pointer : my_false_type 21 | { 22 | }; 23 | 24 | template 25 | struct is_pointer : my_true_type 26 | { 27 | }; 28 | 29 | int main() 30 | { 31 | std::cout << typeid(is_pointer::type).name() << std::endl; 32 | std::cout << is_pointer::value << std::endl; 33 | 34 | return 0; 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /les04_factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | struct factorial 5 | { 6 | static int const value = N * factorial::value; 7 | }; 8 | 9 | template <> 10 | struct factorial<1> 11 | { 12 | static int const value = 1; 13 | }; 14 | 15 | 16 | int main() 17 | { 18 | // creat two different types using the integers 1 and 2 19 | std::cout << factorial<10>::value << std::endl; 20 | 21 | factorial<2> f2; 22 | return 0; 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /les04_key.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | struct make_map 6 | { 7 | typedef ; 8 | }; 9 | 10 | int main() 11 | { 12 | std::cout << typeid(add_pointer::type).name() << std::endl; 13 | std::cout << typeid(add_pointer::type>::type).name() << std::endl; 14 | add_pointer::type foo= 0; 15 | 16 | return 0; 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /les04_partial_specialization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | struct if_ 6 | { 7 | typedef T1 type; 8 | }; 9 | 10 | template 11 | struct if_ 12 | { 13 | typedef T2 type; 14 | }; 15 | 16 | int main() 17 | { 18 | std::cout << typeid(if_).name() << std::endl; 19 | std::cout << typeid(if_).name() << std::endl; 20 | 21 | return 0; 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /les04_recursive_remove_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // main template is the case where it is not a pointer so nothing should happen 5 | // just return the type 6 | template 7 | struct recursive_remove_pointer 8 | { 9 | typedef T type; 10 | }; 11 | 12 | // specialized template is the case where it is a pointer and we return the base 13 | // type 14 | template 15 | struct recursive_remove_pointer 16 | { 17 | typedef typename recursive_remove_pointer::type type; 18 | }; 19 | 20 | int main() 21 | { 22 | std::cout << typeid(recursive_remove_pointer::type).name() << std::endl; 23 | 24 | return 0; 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /les04_remove_pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // main template is the case where it is not a pointer so nothing should happen 5 | // just return the type 6 | template 7 | struct remove_pointer 8 | { 9 | typedef T type; 10 | }; 11 | 12 | // specialized template is the case where it is a pointer and we return the base 13 | // type 14 | template 15 | struct remove_pointer 16 | { 17 | typedef T type; 18 | }; 19 | 20 | int main() 21 | { 22 | std::cout << typeid(remove_pointer::type).name() << std::endl; 23 | std::cout << typeid(remove_pointer::type).name() << std::endl; 24 | std::cout << typeid(remove_pointer::type).name() << std::endl; 25 | 26 | return 0; 27 | } 28 | 29 | 30 | -------------------------------------------------------------------------------- /les04_type.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | struct add_pointer 6 | { 7 | typedef T* type; 8 | }; 9 | 10 | int main() 11 | { 12 | std::cout << typeid(add_pointer::type).name() << std::endl; 13 | std::cout << typeid(add_pointer::type>::type).name() << std::endl; 14 | add_pointer::type foo= 0; 15 | 16 | return 0; 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /les05_SFINAE_enable_if.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | template 9 | struct is_equal : std::false_type 10 | { 11 | }; 12 | 13 | // specialization 14 | template 15 | struct is_equal : std::true_type 16 | { 17 | }; 18 | 19 | template 20 | struct is_random_access_iterator 21 | : is_equal< 22 | typename std::iterator_traits::iterator_category, 23 | std::random_access_iterator_tag 24 | > 25 | { 26 | }; 27 | 28 | template 29 | struct enable_if 30 | { 31 | }; 32 | 33 | //if the arguement is true we get an embedded typedef 34 | template 35 | struct enable_if 36 | { 37 | typedef T type; 38 | }; 39 | 40 | template 41 | typename enable_if::value>::type 42 | my_advance(Iter& it, int n) 43 | { 44 | while(n--) 45 | ++it; 46 | //diagnostic print 47 | std::cout << " my_advance used loop; it did not have a random_access_iterator" 48 | << std::endl; 49 | } 50 | 51 | template 52 | typename enable_if::value>::type 53 | my_advance(Iter& it, int n) 54 | { 55 | it += n; 56 | //diagnostic print 57 | std::cout << " my_advance used const; it had a random_access_iterator" 58 | << std::endl; 59 | } 60 | 61 | template 62 | bool is_palindrome(Container s) 63 | { 64 | auto middle = s.begin(); 65 | //std::advance( middle, s.size() / 2 ); 66 | my_advance( middle, s.size() / 2 ); 67 | return std::equal( s.begin(), middle, s.rbegin() ); 68 | } 69 | 70 | 71 | int main() 72 | { 73 | std::vector l = {'m', 'a', 'd', 'a', 'm'}; 74 | std::cout << " palindrome = " << is_palindrome(l) << ", "; 75 | for(auto& i : l) std::cout << i ; // print the vector as a word 76 | std::cout << std::endl; 77 | 78 | std::list t = {'t', 'e', 's', 't'}; 79 | std::cout << " palindrome = " << is_palindrome(t) << ", "; 80 | for(auto& i : t) std::cout << i ; // print the list as a word 81 | std::cout << std::endl; 82 | return 0; 83 | } 84 | 85 | -------------------------------------------------------------------------------- /les05_SFINAE_negate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | //using a double ends up using SFINAE to substitute an int since typedef is not 9 | //defined 10 | //negate an integer or anything else 11 | 12 | struct foo 13 | { 14 | foo(int i) : data(i) {} 15 | 16 | foo negate() { return foo(-1); } 17 | 18 | int data; 19 | }; 20 | 21 | //Whenever I want to negate an integer 22 | int negate(int i) 23 | { 24 | return -i; 25 | } 26 | 27 | //Not an integer return a type that is defined by the type itself 28 | template 29 | typename T::return_value negate(T v) 30 | { 31 | return v.negate(); 32 | } 33 | 34 | int main() 35 | { 36 | char c ='H' ; 37 | std::string s; 38 | std::cout << "negate(true) = " << negate(true) << std::endl; 39 | std::cout << "negate(5.2) = " << negate(5.2) << std::endl; 40 | std::cout << "negate(2) = " << negate(2) << std::endl; 41 | 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /les05_palindrome_using_iterator_tag.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | // rand access iterator have std::random_access_iterator_tag 9 | 10 | template 11 | struct is_equal : std::false_type 12 | { 13 | }; 14 | 15 | // specialization 16 | template 17 | struct is_equal : std::true_type 18 | { 19 | }; 20 | 21 | template 22 | struct is_random_access_iterator 23 | : is_equal< 24 | typename std::iterator_traits::iterator_category, 25 | std::random_access_iterator_tag 26 | > 27 | { 28 | }; 29 | 30 | template 31 | void my_advance(Iter& it, int n, std::false_type) 32 | { 33 | while(n--) 34 | ++it; 35 | std::cout << " my_advance used loop; it did not have a random_access_iterator" 36 | << std::endl; 37 | } 38 | 39 | template 40 | void my_advance(Iter& it, int n, std::true_type) 41 | { 42 | it += n; 43 | std::cout << " my_advance used const; it had a random_access_iterator" 44 | << std::endl; 45 | } 46 | 47 | template 48 | void my_advance(Iter& it, int n) 49 | { 50 | return my_advance(it, n, 51 | typename is_random_access_iterator::type()); 52 | } 53 | 54 | 55 | template 56 | bool is_palindrome(Container s) 57 | { 58 | auto middle = s.begin(); 59 | //std::advance( middle, s.size() / 2 ); 60 | my_advance( middle, s.size() / 2 ); 61 | return std::equal( s.begin(), middle, s.rbegin() ); 62 | } 63 | 64 | int main() 65 | { 66 | std::vector l = {'m', 'a', 'd', 'a', 'm'}; 67 | std::cout << " palindrome = " << is_palindrome(l) << ", "; 68 | for(auto& i : l) std::cout << i ; // print the vector as a word 69 | std::cout << std::endl; 70 | 71 | std::list t = {'t', 'e', 's', 't'}; 72 | std::cout << " palindrome = " << is_palindrome(t) << ", "; 73 | for(auto& i : t) std::cout << i ; // print the list as a word 74 | std::cout << std::endl; 75 | return 0; 76 | } 77 | 78 | -------------------------------------------------------------------------------- /les06_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | bool is_odd(int v) 9 | { 10 | return (v % 2) != 0; 11 | } 12 | int main() 13 | { 14 | std::vector v = { 1, 4, 3, 6, 8, 9, 10}; 15 | std::cout << std::count_if(v.begin(), v.end(), is_odd) << std::endl; 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /les06_2_my_count_if.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | bool is_odd(int v) 9 | { 10 | return (v % 2) != 0; 11 | } 12 | 13 | template 14 | int my_count_if(Iterator b, Iterator e, F f) 15 | { 16 | int count = 0; 17 | while (b != e) 18 | { 19 | if (f(*b)) 20 | ++count; 21 | ++b; 22 | } 23 | return count; 24 | 25 | } 26 | 27 | int main() 28 | { 29 | std::vector v = { 1, 4, 3, 6, 8, 9, 10}; 30 | std::cout << std::count_if(v.begin(), v.end(), is_odd) << std::endl; 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /les06_3_string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | //function object 9 | 10 | // used this to avoid len as a global 11 | struct shorter_than 12 | { 13 | //explicit insures that int is not converted to shorter_than type 14 | explicit shorter_than(int l) : len(l) {} 15 | 16 | bool operator()(std::string s) 17 | { 18 | return s.size() < len; 19 | } 20 | 21 | int len; 22 | 23 | }; 24 | 25 | int main() 26 | { 27 | std::vector v = { "abcdefg", "a", "decfg", "df"}; 28 | 29 | // created an instance of shorter_than intialized with 3, invokes 30 | // corresponding constructor which expects one integer 31 | // use s as if it is a function, invoking the operator which expects a 32 | // string 33 | // 34 | // shorter_than s(3); 35 | // std::cout << s("abc") << std::endl; //produces false 36 | // std::cout << s("ab") << std::endl; //produces true 37 | 38 | std::cout << count_if(v.begin(), v.end(), shorter_than(3)) << std::endl; 39 | 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /les06_4_lambda.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | std::vector v = { "abcdefg", "a", "decfg", "df"}; 11 | 12 | int len = 3; 13 | std::cout 14 | << std::count_if(v.begin(), v.end(), 15 | [len](std::string s) 16 | { 17 | return s.size() < len; 18 | }) 19 | << std::endl; 20 | 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /les06_4_lambda_int.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | std::vector v = {1, 2, 0, 10, 23, 4}; 11 | 12 | int len = 3; 13 | std::cout 14 | << std::count_if(v.begin(), v.end(), 15 | [len](int i) 16 | { 17 | return i < len; 18 | }) 19 | << std::endl; 20 | 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /les06_5_accumulate.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | //uses accumulate that takes a function 10 | int main() 11 | { 12 | //using accumulate with lambda to find the sum 13 | 14 | std::vector v = { 2, 4, 3, 6, 8, 9, 10 }; 15 | 16 | std::cout 17 | << std::accumulate(v.begin(), v.end(), 0, 18 | [](int sum, int val) { return sum + val; }) 19 | << std::endl; 20 | 21 | //using accumulate to find the number of characters in shortest string 22 | std::vector vs = { "abcdefg", "abc", "decfg", "df"}; 23 | std::cout 24 | << std::accumulate(vs.begin(), vs.end(), 25 | std::numeric_limits::max(), 26 | [](int shortest, std::string s) -> int 27 | { 28 | return (shortest > s.size() ? s.size() : shortest); 29 | }) 30 | << std::endl; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /les07_1_transform.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | 12 | std::vector v = {1, 2, 3, 5, 9, 42}; 13 | 14 | int n =3; 15 | std::transform(v.begin(), v.end(), v.begin(), 16 | [n](int val) 17 | { 18 | return val * n; 19 | }); 20 | 21 | for(int i : v) 22 | std::cout << i << std::endl; 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /les07_2_bind.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int add(int a, int b) 11 | { 12 | return a + b; 13 | } 14 | 15 | int main() 16 | { 17 | 18 | std::vector v = {1, 2, 3, 5, 9, 42}; 19 | 20 | int n =3; 21 | std::transform(v.begin(), v.end(), v.begin(), 22 | std::bind(&add, n, std::placeholders::_1 )); 23 | 24 | for(int i : v) 25 | std::cout << i << std::endl; 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /les07_3_bind.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int add(int a, int b) 11 | { 12 | return a + b; 13 | } 14 | 15 | template 16 | void print(T t) 17 | { 18 | std::cout << t << std::endl; 19 | } 20 | 21 | int main() 22 | { 23 | 24 | int n =3; 25 | print(std::bind(&add, std::placeholders::_1, n)(3)); // 6 26 | print(std::bind(&add, std::placeholders::_3, n)(3, 4, 5)); // 8 27 | print(std::bind(&add, std::placeholders::_3, std::placeholders::_2)(3, 4, 5)); // 9 28 | 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /les07_4_secret.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | template 11 | void print(T t) 12 | { 13 | std::cout << t << std::endl; 14 | 15 | } 16 | 17 | // this is secret; I don't see it; its binary library 18 | int secret(int(*f)(int)) 19 | { 20 | return f(42); 21 | } 22 | 23 | // my code, a global function 24 | int foo(int val) 25 | { 26 | return 2 * val; 27 | } 28 | 29 | int main() 30 | { 31 | print (secret(&foo)); 32 | // or instead of passing a global function pass a lambda 33 | print (secret([](int val) -> int {return 3 * val; })); 34 | //adding a capture gives a compile error 35 | // print (secret([n](int val) -> int {return 3 * val; })); 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /les07_5_secret_std_function.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | template 11 | void print(T t) 12 | { 13 | std::cout << t << std::endl; 14 | 15 | } 16 | 17 | // this is secret; I don't see it; its binary library 18 | int secret(std::functionf) 19 | { 20 | return f(42); 21 | } 22 | 23 | // my code, a global function 24 | int foo(int val) 25 | { 26 | return 2 * val; 27 | } 28 | 29 | int main() 30 | { 31 | int n = 4; 32 | print (secret(&foo)); 33 | // or instead of passing a global function pass a lambda 34 | print (secret([](int val) -> int {return 3 * val; })); 35 | // adding a capture gives a compile error 36 | print (secret([n](int val) -> int {return n * val; })); 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /les08_1_call_by_value.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | template 11 | void print(T t) 12 | { 13 | std::cout << t << std::endl; 14 | 15 | } 16 | 17 | int foo(int value) 18 | { 19 | print(value); 20 | ++value; 21 | print(value); 22 | return value; 23 | } 24 | 25 | int main() 26 | { 27 | int i = 42; 28 | int j = foo(i); 29 | print(i); 30 | print(j); 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /les08_2_call_by_reference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | template 11 | void print(T t) 12 | { 13 | std::cout << t << std::endl; 14 | 15 | } 16 | 17 | int foo(int& value) 18 | { 19 | print(value); 20 | ++value; 21 | print(value); 22 | return value; 23 | } 24 | 25 | int main() 26 | { 27 | int i = 42; 28 | int j = foo(i); 29 | print(i); 30 | print(j); 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /les08_3_vector_by_const_ref.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | template 11 | void print(T t) 12 | { 13 | std::cout << t << std::endl; 14 | 15 | } 16 | 17 | void foo(std::vector const& v) 18 | { 19 | for (int val : v) 20 | print(val+1); 21 | } 22 | 23 | int main() 24 | { 25 | 26 | std::vector v = { 1, 2, 3, 45, 67 }; 27 | foo(v); 28 | for (int val : v) 29 | print(val); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /les08_4_move_semantics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | template 11 | void print(T t) 12 | { 13 | std::cout << t << std::endl; 14 | 15 | } 16 | 17 | // can now return a vector but with c++11 only the pointer info is returned 18 | // so vector is not copied 19 | 20 | std::vector foo(std::vector v) 21 | { 22 | print("pointer of vector in function foo"); 23 | print(v.data()); //prints pointer of vector 24 | for (int val : v) 25 | print(val); 26 | return v; 27 | } 28 | 29 | int main() 30 | { 31 | 32 | std::vector v = { 1, 2, 3, 45, 67 }; 33 | print("pointer of v in main before calling foo"); 34 | print(v.data()); 35 | std::vector v1 = foo(v); 36 | print("pointer of v1 in main after calling foo"); 37 | print(v1.data()); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /les08_5_move_semantics_rvalue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | template 11 | void print(T t) 12 | { 13 | std::cout << t << std::endl; 14 | 15 | } 16 | 17 | // can now return a vector but with c++11 only the pointer info is returned 18 | // so vector is not copied 19 | 20 | std::vector foo(std::vector && v) 21 | { 22 | print(v.data()); 23 | return std::move(v); 24 | } 25 | 26 | int main() 27 | { 28 | 29 | std::vector v = { 1, 2, 3, 45, 67 }; 30 | print(v.data()); 31 | 32 | std::vector v1 = foo(foo(std::move(v))); 33 | print(v1.data()); 34 | print(v.data()); // is zero because data has been moved from here 35 | } 36 | 37 | -------------------------------------------------------------------------------- /les10_slides_threading.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STEllAR-GROUP/cpp_lectures_2016/e7332fb8c4df5f202abdcb4756e367de125a69f9/les10_slides_threading.pdf --------------------------------------------------------------------------------