// ========================================================================== // $Id: captures.cpp,v 1.1 2013/11/10 02:28:08 jlang Exp $ // CSI2372 example Code for lecture 13 - C++11 // ========================================================================== // (C)opyright: // // Jochen Lang // SITE, University of Ottawa // 800 King Edward Ave. // Ottawa, On., K1N 6N5 // Canada. // http://www.site.uottawa.ca // // Creator: jlang (Jochen Lang) // Email: jlang@site.uottawa.ca // ========================================================================== // $Log: captures.cpp,v $ // Revision 1.1 2013/11/10 02:28:08 jlang // Added examples on bind and lambdas // // ========================================================================== #include #include using std::cout; using std::endl; using std::string; int main() { int a=0, b=1, c=1; string article = "A "; string noun = "string"; cout << "a = " << a; auto val_cap = [=] { return a;}; ++a; // will not affect value capture! cout << " -- " << a << endl; cout << "val_cap(): " << val_cap() << endl; cout << "a = " << a << endl; cout << "b = " << b; auto ref_cap = [&] { return ++b;}; cout << " -- " << b; ++b; cout << " -- " << b << endl; cout << "ref_cap(): " << ref_cap() << endl; cout << "b = " << b << endl; cout << "c = " << c; auto val_cap_mutable = [=] () mutable { return ++c;}; --c; // will not affect value capture! cout << " -- " << c << endl; cout << "val_cap_mutable(): " << val_cap_mutable() << endl; cout << "c = " << c << endl; cout << article << noun << endl; auto mixed = [=,&article] { article="The "; return article+noun; }; cout << "Mixed: " << mixed() << endl; cout << article << noun << endl; return 0; }