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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
760
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
20260807_第6回_関東kaggler会LT_claw系bot xangiと始める、"寂しくない" kaggle
sugupoko
0
270
AIコーディングの次。コードレビューと理解負荷を解消して組織の開発生産性を高める
moongift
PRO
2
2.5k
Software Supply Chain Attackからクラウド環境を守るためにできること
lhazy
2
260
SmartHR Engineering Team Deck
smarthr
1
2.1k
LanceDB入門
mocobeta
7
490
同じWAFが、攻撃の“形”は弾く── 正当な“形”の不正は通す
kuroneko13
0
190
【CEDEC2026】Creative Approaches to Localizing the Dialects and Unique Speech of Umamusume: Pretty Derby Characters in English
cygames
PRO
3
16k
Data Hubグループ 紹介資料
sansan33
PRO
0
3.2k
カートの信頼性を担保するWireMockを使ったe2eテスト
ykagano
0
260
研究開発部の紹介 / Sansan R&D Profile
sansan33
PRO
4
24k
Forza Horizon 6 のテレメトリ機能で 自動運転に使えそうな学習データを集める話
henjin0
0
170
強化学習「理論」入門
enakai00
3
3.6k
Featured
See All Featured
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
490
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Designing Powerful Visuals for Engaging Learning
tmiket
1
490
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
890
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Prompt Engineering for Job Search
mfonobong
0
400
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Are puppies a ranking factor?
jonoalderson
1
3.8k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
280
Practical Orchestrator
shlominoach
191
12k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
240
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ͷͭ͘Γ͔ͨ