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.5k
ripgrep をライブラリとして使う
rhysd
0
740
port-monolith-to-wasm-for-chrome-extension
rhysd
0
550
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
Why is RC4 still being used?
tamaiyutaro
0
230
どうして今サーバーサイドKotlinを選択したのか
nealle
0
130
From Prompt Engineering to Loop Engineering
shibuiwilliam
1
290
PostgreSQL 19 新機能概要 OSC Hokkaido 2026
nori_shinoda
0
260
AI Agentをシステムに組み込む前にゆるく向き合ってみる
hayama17
0
170
AI時代における最適なQA組織の作り方
ymty
3
210
AIペネトレーションテスト・ セキュリティ検証「AgenticSec」紹介資料
laysakura
2
7.7k
“ID沼入口” - 基本とセキュリティから始める、考え続けるためのID管理技術勉強会 告知&イントロ
ritou
0
230
10年間のブログ発信を振り返って見えたWebアプリケーションエンジニアとしての軌跡
stefafafan
0
190
AWS Security Hub CSPMの成功・失敗体験
cmusudakeisuke
0
600
從開發到部署全都交給 AI:實作 AI 驅動的自動化流程
appleboy
0
190
MySQL & MySQL HeatWave Report - June 2026
freshdaz
0
220
Featured
See All Featured
Between Models and Reality
mayunak
4
360
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
180
A designer walks into a library…
pauljervisheath
211
24k
Navigating Weather and Climate Data
rabernat
0
250
Designing Experiences People Love
moore
143
24k
Building an army of robots
kneath
306
46k
AI: The stuff that nobody shows you
jnunemaker
PRO
8
740
The Spectacular Lies of Maps
axbom
PRO
1
830
The Invisible Side of Design
smashingmag
301
52k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
160
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.7k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
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ͷͭ͘Γ͔ͨ