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
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
The NotImplementedError Problem in Ruby
koic
1
920
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
180
AIを活用したE2Eテスト実装効率化のあゆみ / ebisu-mobile-14-kotetu
kotetuco
0
130
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
140
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
210
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
150
依存関係から依存物へ―Dependencyという言葉の歴史をひも解く
j_lee
0
130
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.3k
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
1
200
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
230
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
220
Featured
See All Featured
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
150
How STYLIGHT went responsive
nonsquared
100
6.2k
Crafting Experiences
bethany
1
190
Are puppies a ranking factor?
jonoalderson
1
3.6k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
Abbi's Birthday
coloredviolet
3
8.2k
KATA
mclloyd
PRO
35
15k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
Site-Speed That Sticks
csswizardry
13
1.2k
Odyssey Design
rkendrick25
PRO
2
710
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
850
What's in a price? How to price your products and services
michaelherold
247
13k
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