Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
MeetingC++ Quiz
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
James
November 23, 2016
Programming
3.3k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
MeetingC++ Quiz
James
November 23, 2016
More Decks by James
See All by James
Conan C/C++ package manager
memsharded
1
1k
Other Decks in Programming
See All in Programming
さぁV100、メモリをお食べ・・・
nilpe
0
150
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
210
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.8k
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
170
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
280
スマートグラスで並列バイブコーディング
hyshu
0
260
AI 輔助遺留系統現代化的經驗分享
jame2408
1
970
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
13k
The NotImplementedError Problem in Ruby
koic
1
920
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
610
Vite+ Unified Toolchain for the Web
naokihaba
0
340
Featured
See All Featured
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
GraphQLとの向き合い方2022年版
quramy
50
15k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.5k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
The agentic SEO stack - context over prompts
schlessera
0
820
Typedesign – Prime Four
hannesfritz
42
3.1k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
370
The SEO identity crisis: Don't let AI make you average
varn
0
500
Chasing Engaging Ingredients in Design
codingconduct
0
230
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
170
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
200
Transcript
1 template <typename T> void P(const T & x) {
std::cout << x; } void foo(const void*) { P("v"); } void foo(const std::string&) { P("s"); } int main() { unsigned int i; for (i = 3; i>0; i--) P(i); for (; i<4; i++) P(i); if (i > -1) P("z"); foo("Conan!"); }
2 template <typename T> void P(const T & x) {
std::cout << x; } // wish VS accepted auto, sigh void foo(const std::string& str, std::function<void(char)> mylambda) { for (auto ch : str) mylambda(ch); } int main() { int num = 1; std::string str("ab"); foo(str, [&](char ch) {P(num++); P(ch);}); foo(str, [=](char ch) mutable {P(num++); P(ch);}); P(num); }
3 template <typename T> void P(const T & x) {
std::cout << x; } template <typename T> void P(int t, const T & x) { std::cout << t << x; } template<typename T, typename ...Args> void P(T t, Args ...args) { P(args...); P(t); } int main() { P(1, "dog", 2, "cat"); }
4 template <typename T> void P(const T & x) {
std::cout << x; } class Foo { public: Foo(int _a) : a(_a) { P(a++); } Foo(Foo& c) : a(c.a) { P(a++); } Foo(Foo&& c) : a(c.a) { P(a++);c.a = 9; } void bar() { P(a); } private: int a; }; int main() { Foo a(1); Foo b(a); Foo c(std::move(b)); Foo d = static_cast<Foo&&>(c); a.bar(); b.bar(); c.bar(); d.bar(); }
5 template <typename T> void P(const T & x) {std::cout
<< x;} int main() { std::vector<int> v{ 1, 2, 3, 4 }; std::iota(v.begin(), v.end(), 2); v.emplace_back(std::accumulate(v.begin(), v.end(), 1)); std::partial_sum(v.begin(), v.end(), v.begin()); for (auto value : v) P(value); }
6 template <typename T> void P(const T & x) {std::cout
<< x;} template <typename T> struct Foo { Foo(T _a, T _b) : a(_a), b(_b) { P(a++); P(b++); } ~Foo() { P(b); P(a); } T a; T b; }; int main() { auto foo2 = std::make_shared<Foo<int>>(1, 2); auto foo1 = std::make_unique<Foo<int>>(5, 6); auto foo3 = std::move(foo1); auto foo4 = std::move(foo2); P(foo3->a); P(foo4->b); }
7 struct Barrier { std::mutex _mutex; std::condition_variable _cv; std::size_t _count;
explicit Barrier(std::size_t count):_count{ count } { } void wait() { std::cout << "1" << std::flush; std::unique_lock<std::mutex> lock{ _mutex }; if (--_count == 0) _cv.notify_all(); else _cv.wait(lock, [this] { return _count == 0; }); std::cout << "2" << std::flush; } }; int main() { Barrier b(4); std::vector<std::thread> v; for (int i = 0;i<3;i++) v.push_back(std::thread([&]() {b.wait();})); std::cout << "z" << std::flush; v.push_back(std::thread([&]() {b.wait();})); for (auto& t : v) t.join(); }
8 #define DECLARE_FOO(class_name)\ struct class_name{ T t1; T t2; \
class_name(T v1, T v2): t1(v1), t2(v2) {} #define DECLARE_BAR(bar, r) void bar(){std::cout<<r;} template <typename T> DECLARE_FOO(Foo) DECLARE_BAR(bar, t1) DECLARE_BAR(baz, t2) }; int main() { std::random_device r; std::default_random_engine e1(r()); std::uniform_int_distribution<int> uniform_dist(1, 50); std::map<int, int> hist; for (int n = 0; n < 50; ++n) hist[uniform_dist(e1)]++; auto a = std::accumulate(std::begin(hist), std::end(hist), 0, [](auto a, auto b) {return a + b.second;}); Foo<int> f(a, a + 1); f.bar(); f.baz(); }
9 struct Foo { int foo; Foo(int foo) : foo(foo)
{} Foo(char foo) : foo(foo + 1) {} Foo(std::string foo) : foo(42) {} Foo() : Foo(0) {} Foo(float foo) : Foo("conan!") {} }; template <typename T> void P(const std::array<T, 3>& list) { for (auto& e : list) std::cout << e.foo + 1; } template <typename T> void P(const std::initializer_list<T>& list) { for (auto& e : list) std::cout << e.foo; } int main() { auto foos = { Foo(1), Foo((char)2), Foo("MeetingC++") }; auto t = std::make_tuple(Foo(), Foo(3.5f)); P(foos); P({ std::get<0>(t), std::get<1>(t) }); }
constexpr auto foo = 1 + 2 / 3 +
1.0; constexpr auto bar(float foo) { return foo * 2 - 3 / 2; } constexpr auto baz = bar(foo - 2); int main() { std::string s; baz > foo ? s += "cat" : s += "pum"; if (sizeof(int) == 4) s += " de"; s += "orc"; for (int i = 0;i<327964;i++) std::random_shuffle(s.begin(), s.end()); std::cout << s; } 10
ANSWERS
1 template <typename T> void P(const T & x) {
std::cout << x; } void foo(const void*) { P("v"); } void foo(const std::string&) { P("s"); } int main() { unsigned int i; for (i = 3; i>0; i--) P(i); for (; i<4; i++) P(i); if (i > -1) P("z"); foo("Conan!"); } 3210123v
2 template <typename T> void P(const T & x) {
std::cout << x; } // wish VS accepted auto, sigh void foo(const std::string& str, std::function<void(char)> mylambda) { for (auto ch : str) mylambda(ch); } int main() { int num = 1; std::string str("ab"); foo(str, [&](char ch) {P(num++); P(ch);}); foo(str, [=](char ch) mutable {P(num++); P(ch);}); P(num); } 1a2b3a4b3
3 template <typename T> void P(const T & x) {
std::cout << x; } template <typename T> void P(int t, const T & x) { std::cout << t << x; } template<typename T, typename ...Args> void P(T t, Args ...args) { P(args...); P(t); } int main() { P(1, "dog", 2, "cat"); } 2catdog1
4 template <typename T> void P(const T & x) {
std::cout << x; } class Foo { public: Foo(int _a) : a(_a) { P(a++); } Foo(Foo& c) : a(c.a) { P(a++); } Foo(Foo&& c) : a(c.a) { P(a++);c.a = 9; } void bar() { P(a); } private: int a; }; int main() { Foo a(1); Foo b(a); Foo c(std::move(b)); Foo d = static_cast<Foo&&>(c); a.bar(); b.bar(); c.bar(); d.bar(); } 12342995
5 template <typename T> void P(const T & x) {std::cout
<< x;} int main() { std::vector<int> v{ 1, 2, 3, 4 }; std::iota(v.begin(), v.end(), 2); v.emplace_back(std::accumulate(v.begin(), v.end(), 1)); std::partial_sum(v.begin(), v.end(), v.begin()); for (auto value : v) P(value); } 2591429
6 template <typename T> void P(const T & x) {std::cout
<< x;} template <typename T> struct Foo { Foo(T _a, T _b) : a(_a), b(_b) { P(a++); P(b++); } ~Foo() { P(b); P(a); } T a; T b; }; int main() { auto foo2 = std::make_shared<Foo<int>>(1, 2); auto foo1 = std::make_unique<Foo<int>>(5, 6); auto foo3 = std::move(foo1); auto foo4 = std::move(foo2); P(foo3->a); P(foo4->b); } 1256633276
7 struct Barrier { std::mutex _mutex; std::condition_variable _cv; std::size_t _count;
explicit Barrier(std::size_t count):_count{ count } { } void wait() { std::cout << "1" << std::flush; std::unique_lock<std::mutex> lock{ _mutex }; if (--_count == 0) _cv.notify_all(); else _cv.wait(lock, [this] { return _count == 0; }); std::cout << "2" << std::flush; } }; int main() { Barrier b(4); std::vector<std::thread> v; for (int i = 0;i<3;i++) v.push_back(std::thread([&]() {b.wait();})); std::cout << "z" << std::flush; v.push_back(std::thread([&]() {b.wait();})); for (auto& t : v) t.join(); } 111z12222
8 #define DECLARE_FOO(class_name)\ struct class_name{ T t1; T t2; \
class_name(T v1, T v2): t1(v1), t2(v2) {} #define DECLARE_BAR(bar, r) void bar(){std::cout<<r;} template <typename T> DECLARE_FOO(Foo) DECLARE_BAR(bar, t1) DECLARE_BAR(baz, t2) }; int main() { std::random_device r; std::default_random_engine e1(r()); std::uniform_int_distribution<int> uniform_dist(1, 50); std::map<int, int> hist; for (int n = 0; n < 50; ++n) hist[uniform_dist(e1)]++; auto a = std::accumulate(std::begin(hist), std::end(hist), 0, [](auto a, auto b) {return a + b.second;}); Foo<int> f(a, a + 1); f.bar(); f.baz(); } 5051
9 struct Foo { int foo; Foo(int foo) : foo(foo)
{} Foo(char foo) : foo(foo + 1) {} Foo(std::string foo) : foo(42) {} Foo() : Foo(0) {} Foo(float foo) : Foo("conan!") {} }; template <typename T> void P(const std::array<T, 3>& list) { for (auto& e : list) std::cout << e.foo + 1; } template <typename T> void P(const std::initializer_list<T>& list) { for (auto& e : list) std::cout << e.foo; } int main() { auto foos = { Foo(1), Foo((char)2), Foo("MeetingC++") }; auto t = std::make_tuple(Foo(), Foo(3.5f)); P(foos); P({ std::get<0>(t), std::get<1>(t) }); } 1342042
constexpr auto foo = 1 + 2 / 3 +
1.0; constexpr auto bar(float foo) { return foo * 2 - 3 / 2; } constexpr auto baz = bar(foo - 2); int main() { std::string s; baz > foo ? s += "cat" : s += "pum"; if (sizeof(int) == 4) s += " de"; s += "orc"; for (int i = 0;i<327964;i++) std::random_shuffle(s.begin(), s.end()); std::cout << s; } 10 core dump