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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
AIキャラアプリkaiwaの低遅延音声通話基盤をどう作ったか - AWS Gravitonで支える低遅延・低コストAI Agent基盤
mogamit
0
170
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
2
530
信頼性について考えてみる(SRE NEXT 2026 miniLT)
hayama17
0
190
The Bowling Game- From Imperative to Functional Programming - Part 1
philipschwarz
PRO
0
330
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
650
どこまでゆるくて許されるのか
tk3fftk
0
490
symfony/aiとlaravel/boost
77web
0
130
AIを活用したE2Eテスト実装効率化のあゆみ / ebisu-mobile-14-kotetu
kotetuco
0
170
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
1
190
Haskell/Servantを通してWebミドルウェアを捉え直す
pizzacat83
1
580
ランチタイムLT会3周年!ランチタイムLT会を3年間続けられたお話
y0hgi
1
140
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
660
Featured
See All Featured
Done Done
chrislema
186
16k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
460
Optimizing for Happiness
mojombo
378
71k
GitHub's CSS Performance
jonrohan
1033
470k
Building Applications with DynamoDB
mza
96
7.1k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
630
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
360
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
240
Documentation Writing (for coders)
carmenintech
77
5.4k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
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