Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
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.7k
ripgrep をライブラリとして使う
rhysd
0
770
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
10Xに技術的負債をもたらした「2つの境界の歪み」その構造と解消への営み
10xinc
0
2.3k
AIエージェントを最高のパートナーに育てる方法|評価と判断軸を育てる5つのステップ
koichiaoki
1
160
時うどん〜Socket.getifaddrsで学ぶネットワーク編 / Tokiudon: The Socket.getifaddrs Edition
coe401_
4
210
リアーキテクチャ後の障害ゼロを目指したShadow Testingの取り組み
nihonbuson
PRO
1
170
2026-09-08 そのJavaモダナイゼーション、AIに丸投げで大丈夫?IBM Bobで変わる品質と効率
yutanonaka
1
120
データ_AIの事業の勝敗をわけるもの
nek0128
1
470
ScotSecure West 2026 - Glasgow
raybugg
0
140
ソフトウェアDNAとクラウドエージェントのススメ
cloudace
0
120
AIによるクリエイティブ生成を行う上での試行錯誤
plaidtech
PRO
0
170
Genieを崇めよ
kameitomohiro
0
170
「ピッケル本」日本語版は4.0(第6版)が出版されるべき / pickaxe4-nagoyark05
kakutani
2
270
AIに任せた品質は、誰が見立てるのか - AI時代のテストマネジメント
nakanao
3
2.7k
Featured
See All Featured
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
700
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.2k
How to Talk to Developers About Accessibility
jct
2
550
Facilitating Awesome Meetings
lara
57
7.1k
Designing for Performance
lara
611
70k
WENDY [Excerpt]
tessaabrams
14
39k
Designing Powerful Visuals for Engaging Learning
tmiket
1
570
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
330
Discover your Explorer Soul
emna__ayadi
2
1.3k
The Language of Interfaces
destraynor
162
27k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Raft: Consensus for Rubyists
vanstee
142
7.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ͷͭ͘Γ͔ͨ