Share
Embed
C++17 std::anyについて
nakameguro_feature.cpp vol.10
https://ebisu-effective-modern-cpp.connpass.com/event/104657/
での資料です!!
Nobuyuki Nishiyama
October 18, 2018
Transcript
15 slides
-
$ TUEBOZ ࣍ ࣗݾհ ࠓ͢͜ͱ * ͷ͍ํ * ͷศརͳ͍ํ ࢀߟࢿྉ
ࣗݾհ ࢁ৴ߦ ͔ΒήʔϜϓϩάϥϚͱͯ͠׆ಈ 1$ɺՈఉ༻ήʔϜػɺεϚϗ ઐֶߍͰήʔϜϓϩάϥϛϯάΛڭ͑Δ ຊిࢠઐֶߍ d ओʹ࣮शಜ όϯλϯήʔϜΞΧσϛʔ d ʮ$ ͰήʔϜΛ࡞ΔʯΈ͍ͨͳ༰Ͱ·Δͬͱतۀ $ Λ৮Γ࢝Ίͨͷ͘Β͍͔Β -
J04ͰήʔϜΛ࡞͍ͬͯ·͢ ࠓ͢͜ͱ $ ͰՃ͞Εͨ* ͷ͍ํͱศརͳ͍ํ ͳʹͿΜ$ ॳ৺ऀͳͷͰποίϛ͓ئ͍͠·͢ ͷ͍ํ ΊͬͪΌ͍͍ࢿྉΛݟ͚ͭͨͷͰɺͦΕΛ͍·͢
IUUQTHJUIVCDPN&[PF3ZPVDQQCPPLCMPCNBTUFSDQQMJCBOZNE ิ #include <any> #include <vector> #include <iostream> int main() { // sd p o auto a = std::make_any<std::vector<int>>({ 0, 1, 2, 3 }); std::cout << std::any_cast<std::vector<int>>(a)[1] -
ͷศརͳ͍ํ * ʹ͍ͭͯΘ͔͖ͬͯͨͷͰɺͲΜͳ࣌ʹ͑Δͷ͔ͷհ ίϯςφͱซͤͯ͏ << std::endl; } #include <iostream>
#include <any> #include <vector> #include <map> int main() { // std::vector auto a = std::vector<std::any>{ -1, 8.5, "Hoge" }; std::cout << std::any_cast<const char*>(a[2]) << std::endl; // std::map auto b = std::map<std::string, std::any>{ { "a", -1 }, { "b", 8.5 }, { "c", "Hoge" }, }; -
ԿͰ֨ೲͰ͖Δίϯςφܕ҆શੑͱ * ͕ແ͍ͷͰTUETFUʹ͑ͣɻ +40/ͷಡΈॻ͖Ͱ͑ͦ͏ ͱจࣈྻͷೖΓཚΕͨΛखؒΛ͔͚ͣʹѻ͑ͦ͏ Πϕϯτॲཧ ήʔϜͰ͋Γ͕ͪͳ˓˓͖͔͚ͬͰॲཧΛ͢ΔΛߟ͑Δ std::cout <<
std::any_cast<double>(b.at("b")) << std::endl; } #include <any> #include <set> int main() { std::set<std::any> s; std::any a; s.insert(a); } LRESULT CALLBACK WndProc(HWND hwnd , UINT msg , WPARAM wp , LPARAM lp) -
8JOEPXTͰΞϓϦΛ࡞Δͱ͖ͷϝοηʔδॲཧΛ͓खຊʹͯ͠ΈΔ { switch (msg) { case WM_DESTROY: // Window <
PostQuitMessage(0); return 0; case WM_LBUTTONDOWN: // DoAnything(); // return 0; } return DefWindowProc(hwnd , msg , wp , lp); } #include <any> #include <functional> #include <vector> // enum { GAME_START, GAME_END, PLAYER_DEAD, }; // struct Item { // r t void event(int event_type) -
{ switch (event_type) { case GAME_START: // r break; default:
break; } } }; // r void EventProc(int event_type) { switch (event_type) { case GAME_START: // r break; default: break; } } int main() { Item a; // t std::vector<std::function<void (int)>> func; -
ؔϝϯόؔΛɺؔϙΠϯλͱͯ͠ొͯ͠ஞ࣍ݺͼग़͢ͷ͙͢Ͱ͖Δ͕ɺͳͷ͕ίʔϧόοΫؔͷʮҾ ʯͷܕɻ $ݴޠͰ࣮͍ͯͨ࣌͠ Ͱͨ͠ ͦ͜ͰҾΛ* ʹͯ͠ΈΔ // func.push_back(std::bind(&Item::event, &a, std::placeholders::_1));
func.push_back(EventProc); // : v r for (auto& f : func) { f(GAME_START); } } #include <functional> #include <vector> #include <any> enum { GAME_START, GAME_END, PLAYER_DEAD, }; // GAME_START i t struct GameStartArg -
{ int a; float b; }; void EventProc(int event_type, std::any&
arg) { switch (event_type) { case GAME_START: { // t o auto& a = std::any_cast<GameStartArg&>(arg); } break; default: break; } } int main() { // t ay std::vector<std::function<void (int, std::any&)>> func; // func.push_back(EventProc); // : t v r auto arg = std::make_any<GameStartArg>(GameStartArg{ -1, 0.5f }); for (auto& f : func) { -
Ҿ༻ͷߏମΛ͍͍ͪͪఆٛ͢Δͷ͕ΊΜͲ͘͘͞ͳͬͨͷͰɺࠓ͜ͷํࣜ f(GAME_START, arg); } } #include <functional> #include <string> #include
<vector> #include <map> #include <any> enum { GAME_START, GAME_END, PLAYER_DEAD, }; // t using Arguments = std::map<std::string, std::any>; void EventProc(int event_type, Arguments& arg) { switch (event_type) { case GAME_START: { // t o auto a = std::any_cast<int>(arg.at("a")); } -
ΊͬͪΌศརɻܕ҆શੑͱɻ 6*ͷ֤छ෦Λѻ͏࣌ break; default: break; } } int main() {
// t ay std::vector<std::function<void (int, Arguments&)>> func; // func.push_back(EventProc); // : t v r auto arg = Arguments{ { "a", -1 }, // int { "b", 0.5 }, // double }; for (auto& f : func) { f(GAME_START, arg); } } -
͋Γ͕ͪͳ6*ॲཧΛॻ͘ͱ͠·͢ #include <vector> #include <memory> // UI struct UIBase {
virtual void draw() = 0; virtual ~UIBase() {} }; // struct UIBox : public UIBase { void draw() { // r } }; struct UICircle : public UIBase { void draw() { // r } }; int main() { // UI ay -
͜ͷ࣍ͷεςοϓͰׂͱࠔΔ 6*#PYͰͷαΠζࢦఆ 6*$JSDMFͰͷܘࢦఆ ৭ࢦఆ 6*ผͷॲཧ͕૿͑ͯΊΜͲ͘ͳΔɻͦ͜Ͱ* ͷग़൪ std::vector<std::shared_ptr<UIBase>> ui; ui.push_back(std::make_shared<UIBox>()); ui.push_back(std::make_shared<UICircle>());
// e n for (auto& obj : ui) { obj->draw(); } } #include <vector> #include <memory> #include <map> #include <string> #include <any> // UI using Params = std::map<std::string, std::any>; -
// UI struct UIBase { virtual void setParams(Params& params) =
0; virtual void draw() = 0; virtual ~UIBase() {} }; // struct UIBox : public UIBase { void setParams(Params& params) { int w = std::any_cast<int>(params.at("width")); int h = std::any_cast<int>(params.at("height")); } void draw() { // r } }; struct UICircle : public UIBase { void setParams(Params& params) { int r = std::any_cast<int>(params.at("radius")); } void draw() { // r } -
6*ΛςΩετ͔Βߏங͢Δ࣌ʹΊͬͪΌศརɻܕ҆શੑͱɻ ࢀߟࢿྉ }; int main() { // UI ay std::vector<std::shared_ptr<UIBase>>
ui; ui.push_back(std::make_shared<UIBox>()); ui.push_back(std::make_shared<UICircle>()); auto params_box = Params{ { "width", 100 }, { "height", 150 }, }; ui[0]->setParams(params_box); auto params_circle = Params{ { "radius", 50 }, }; ui[1]->setParams(params_circle); // e n for (auto& obj : ui) { obj->draw(); } }