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
440
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
750
port-monolith-to-wasm-for-chrome-extension
rhysd
0
560
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.6k
Other Decks in Technology
See All in Technology
AIツールを導入しても生産性はあがらない? カオナビが直面した 3つの壁と乗り越え方。/ Overcoming 3 Barriers to AI-Driven Productivity at kaonavi
kaonavi
0
160
「顧客の声を聞かなければ何も始まらない」 ── 顧客の声から生まれた『AI返信補助機能』の開発プロセス / AICon2026_shikata_imai
rakus_dev
1
270
プロダクト開発組織の現在地(Ver.2026/07) / product-organization
kaonavi
0
390
書籍セキュアAPIについて
riiimparm
0
220
AI時代におけるエンジニアの新たな役割──FDEとクオリアの探求/登壇資料(戸井田 裕貴)
hacobu
PRO
0
310
全社でのソフトウェアサプライチェーン攻撃対策をやってみた with Takumi Guard
z63d
0
270
”AIを使う” から ”AIに任せる” へ ─ 開発プロセスを再設計してAIを組織標準にするまで
cyberagentdevelopers
PRO
1
140
ファミコンでPHPを動かす / PHP on the Famicom
tomzoh
2
600
ダッシュボード"開発"について 〜使われるダッシュボードのつくりかた〜
kimichan
0
200
インフラと開発の垣根を超えていき!〜元AWSインフラエンジニアがAWS開発で奮闘している話〜
hatahata021
3
390
なぜ、あなたのエージェントは言うことを聞かないのか
segavvy
1
360
キャリアLT会#3
beli68
2
240
Featured
See All Featured
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
Test your architecture with Archunit
thirion
1
2.3k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
The agentic SEO stack - context over prompts
schlessera
0
850
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
410
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
380
How to build a perfect <img>
jonoalderson
1
5.8k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
810
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
400
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
150
Making Projects Easy
brettharned
120
6.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ͷͭ͘Γ͔ͨ