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
Boost.Variant tips
Search
Linda_pp
May 26, 2014
Technology
450
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Boost.Variant tips
Linda_pp
May 26, 2014
More Decks by Linda_pp
See All by Linda_pp
actionlint の Linter 設計
rhysd
7
6.6k
ripgrep をライブラリとして使う
rhysd
0
760
port-monolith-to-wasm-for-chrome-extension
rhysd
0
570
Fuzzing Rust Text Editor
rhysd
1
3.1k
Vim compiled to WebAssembly
rhysd
5
2.4k
about-neovim-0.4.0-floating-window
rhysd
3
2.4k
reply.vim
rhysd
0
1.4k
Vim ported to WebAssembly (VimConf 2018)
rhysd
4
3.6k
go-selfupdate-github で ツールを自己アップデートする
rhysd
5
4.7k
Other Decks in Technology
See All in Technology
会計事務所と顧問先の契約関係をOIDC・OAuthで表現する
terara
0
580
AI駆動開発はどこまで来たのか? ファインディの最新実態調査で読み解く現在地 Devin Con Tokyo
akiratom
5
2.3k
Bet AI Day 2026丨バクラク Autopilot、業務システムの再設計
layerx
PRO
1
750
VPCでもFloatingIPを使いたいんだ
y_kotani
1
140
全社に広がるMCPサーバーを、 どう安全に管理するか MCPass開発の舞台裏
mtpooh
3
230
[2026-08-25]AIによる自動化と人の介入、その狭間で揺れる信頼性についての俺の思いを、波よ聞いてくれ
tosite
0
130
【Oracle AI Spotlight ウェビナー】AWSか、Azureか、Google Cloudか。その議論にオラクルを含める意義。
oracle4engineer
PRO
1
180
現場の暗黙知を継承するAIエージェント — 対話から生まれる長期記憶と Skills
atsukish
0
210
bet_ai_day_2026_session02
agenticsec
1
650
AWSとAzureのマルチクラウド活用における強い味方___AWS_Kiroを使った二刀流スキル作成.pdf
duelist2020jp
1
170
Self Healing Rollouts: Automating Production Fixes with Agentic AI
kdubois
0
120
20260903 Tokyo Jazug Night #62 | Azure エンジニアよ、 その環境は本当にセキュアか?
olivia_0707
0
220
Featured
See All Featured
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.6k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
42k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
520
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
370
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
460
KATA
mclloyd
PRO
35
15k
Automating Front-end Workflow
addyosmani
1369
210k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
910
Into the Great Unknown - MozCon
thekraken
41
2.7k
Transcript
#PPTU7BSJBOUUJQT 5XJUUFS!-JOEB@QQ (JUIVC!SIZTE
CPPTUWBSJBOUͱ w ܕ҆શͳڞ༻ମʢVOJPOʣ w /FWFSFNQUZHVBSBOUFF ! ! ! ! !
w IUUQTTJUFTHPPHMFDPNTJUFCPPTUKQUJQTWBSJBOU 1 #include <string>! 2 #include <boost/variant/variant.hpp>! 3 ! 4 int main()! 5 {! 6 // int ͱ string ͷͲͪΒ֨ೲՄೳ! 7 boost::variant<int, std::string> v;! 8 ! 9 v = 42;! 10 v = "aaa";! 11 ! 12 return 0;! 13 }
WBSJBOUͷΞΫηεɿHFU 1 boost::variant<int, std::string> v;! 2 ! 3 switch(v.which()) {!
4 // 1ͭͷཁૉ͕ೖ͍ͬͯΔͱ͖! 5 case 0:! 6 int i = boost::get<int>(v); // ࣦഊ͢Δͱྫ֎! 7 break;! 8 ! 9 // 2ͭͷཁૉ͕ೖ͍ͬͯΔͱ͖! 10 case 1:! 11 // ϙΠϯλΞΫηε! 12 std::string *d = boost::get<double>(&v)! 13 break;! 14 }
CPPTUHFUͷ w XIJDI ͰࣄલʹதΛௐΔˠϚδοΫφϯόʔ w ϙΠϯλόʔδϣϯˠੜϙΠϯλ৮Γͨ͘ͳ͍ w ྫ֎όʔδϣϯˠຖྫ֎͕ඈͿͷʜ w HFU5
ͷ5WBSJBOU͕อ࣋͞Ε͍ͯͳ͍ܕͰ ίϯύΠϧ௨ͬͯ͠·͏
CPPTUHFUͷվળ 1 ! 2 // boost::optional Ͱฦ͢όʔδϣϯΛͭ͘Δ! 3 template<class T,
class Variant>! 4 boost::optional<Tconst&> get(Variant const& v);! 5 ! 6 boost::variant<int, std::string> v;! 7 ! 8 if (auto maybe_int = get<int>(v)) {! 9 auto const&value = *maybe_int;! 10 }
CPPTUHFUͷվળ 8 template<class T, class... Args>! 9 inline boost::optional<T const&>
get(boost::variant<Args...> const& v)! 10 {! 11 // Args... ͷதʹ T ͕ແ͚ΕΤϥʔ! 12 static_assert(! 13 is_included<T, Args...>::value,! 14 "get_as(const&): T is not included in the variant."! 15 );! 16 ! 17 T const* const ptr = boost::get<T>(&v);! 18 if (ptr) {! 19 return {*ptr};! 20 } else {! 21 return boost::none;! 22 }! 23 }
WBSJBOUͷΞΫηεɿWJTJUPS 1 struct to_string! 2 : boost::static_visitor<std::string> {! 3 template<class
T>! 4 std::string operator()(T v)! 5 {! 6 return std::to_string(v);! 7 }! 8 };! 9 ! 10 int main()! 11 {! 12 boost::variant<int, double> v;! 13 auto s1 = ! ! ! ! ! boost::apply_visitor(to_string{}, v);! 14 }
WJTJUPSͷվળɿϥϜμࣜ 1 struct to_string : boost::static_visitor<std::string> {! 2 ! 3
template<class T>! 4 std::string operator()(T v)! 5 {! 6 return std::to_string(v);! 7 }! 8 };! 9 ! 10 int main()! 11 {! 12 boost::variant<int, double> v;! 13 auto s1 = boost::apply_visitor(to_string{}, v);! 14 } 1 int main()! 2 {! 3 boost::variant<int, double> v;! 4 auto s1 = apply_lambda(! 5 [](auto const& v){ return std::to_string(v); }! 6 , v! 7 );! 8 }
WJTJUPSͷվળɿϥϜμࣜ 1 template<class Lambda, class Result>! 2 struct lambda_wrapped_visitor! 3
: public boost::static_visitor<Result>! 4 , public Lambda {! 5 ! 6 lambda_wrapped_visitor(Lambda const& l) noexcept! 7 : Lambda(l)! 8 {}! 9 };! 10 ! 11 template<class Lambda, class Head, class... Tail>! 12 inline auto apply_lambda(Lambda const& l, boost::variant<Head, Tail...> const& variant)! 13 {! 14 return boost::apply_visitor(! 15 detail::lambda_wrapped_visitor<! 16 Lambda,! 17 decltype(std::declval<Lambda>()(std::declval<Head const&>()))! 18 >{l}! 19 , variant);! 20 }
ཁૉҎ্ͷWBSJBOU 1 using holder =! 2 boost::mpl::vector< Type1! 3 ...!
4 , Type40 >;! 5 ! 6 typename boost::make_variant_over<holder>::type v; w σϑΥϧτཁૉɿʢWBSJBOU5 5 ʜ 5ʣ w ͨͩ͠Λ૿͗͢͠ΔͱίϯύΠϧ͕࣌ؒͻͲ͘ ͳΔ
UJQT·ͱΊ w CPPTUHFU ͷΓΛPQUJPOBMʹͨ͠ w WJTJUPSʹϥϜμࣜΛ͑ΔΑ͏ʹ w ཁૉҎ্Λ࣋ͭWBSJBOUͷͭ͘Γ͔ͨ