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
みんな代替トークン使とる。使てへんのお前だけ。 / alternative tokens
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Miutsuru kariya
January 29, 2020
Programming
2.1k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
みんな代替トークン使とる。使てへんのお前だけ。 / alternative tokens
C++MIX #7 の発表資料です。
Miutsuru kariya
January 29, 2020
More Decks by Miutsuru kariya
See All by Miutsuru kariya
みんな大好き!using ディレクティブ! / using directive
kariyamitsuru
1
1.6k
C++17の新機能 落穂拾い / new features of C++17 - gleaner
kariyamitsuru
0
1.5k
C++17の新機能 optional編 / new features of C++17 - optional
kariyamitsuru
1
3.1k
C++17の新機能 アロケータ編 / new features of C++17 - allocator
kariyamitsuru
0
3.9k
C++17の新機能 連想コンテナ編 / new features of C++17 - associative containers
kariyamitsuru
1
1.2k
C++17の新機能 言語編 / new features of C++17 - language
kariyamitsuru
0
480
C++14 の新機能 ライブラリ編 / new features of C++14 - Library
kariyamitsuru
0
780
Other Decks in Programming
See All in Programming
ビデオ通話が繋がる0.2秒で何が起きているのか
supurazako
2
160
複数の Claude Code が"放置"されてしまう問題をCLI ダッシュボードを自作して解決した話
sumihiro3
0
580
これって Effect でできたのでは? / TSKaigi Mashup Kansai #2
susisu
0
130
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
500
PHP Application における Kubernetes 内 gRPC 通信
ganchiku
0
570
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
140
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
420
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
120
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
130
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
450
OpenSpecのproposalにbrainstormingを持たせてみた
tigertora7571
1
160
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
3
1.3k
Featured
See All Featured
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
The Spectacular Lies of Maps
axbom
PRO
1
880
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
410
Google's AI Overviews - The New Search
badams
0
1.1k
Skip the Path - Find Your Career Trail
mkilby
1
170
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.1k
My Coaching Mixtape
mlcsv
0
180
Ruling the World: When Life Gets Gamed
codingconduct
0
290
Practical Orchestrator
shlominoach
191
11k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.1k
Transcript
みんな代替トークン使とる。 使てへんのお前だけ。 2020/1/29 鳥頭かりやマン 1
初心者向けネタ発表です。 「そんなんみんな知っとるが」など、その 筋の方からのツッコミは無しの方向でお 願いします… 2
代替トークンとは? 3
代替トークンとは? 「代替」の「トークン」 まんま… なお、よく「ダイグラフ」と 呼ばれたりもするが… 4
代替トークンとは? 代替 本来 代替 本来 代替 本来 <% { and
&& xor ^ %> } bitand & xor_eq ^= <: [ and_eq &= not ! :> ] or || not_eq != %: # bitor | compl ~ %:%: ## or_eq |= 5 見ての通り、必ずしも「ダイ」(2文字)ではなく、 キーワード型の物も結構ある。 これらの名前も予約語なので、例えば and や or と言った関数名や変数名は使えない。 ダ イ グ ラ フ で は な い 。 代 替 ト ー ク ン と 呼 べ 。
代替トークンとは? 代替 本来 代替 本来 代替 本来 <% { and
&& xor ^ %> } bitand & xor_eq ^= <: [ and_eq &= not ! :> ] or || not_eq != %: # bitor | compl ~ %:%: ## or_eq |= 6 なお、「トークン」と名前がついている通り、コンパイラが ソースをトークン化する際に処理されるので、文字列中 や他の識別子中では代替トークンとはみなされない。 まぁ当たり前っちゃあ当たり前なんだけど…
使用例 7
使用例 使用前 #include <iostream> int main() { int a[] =
{ 114, 514, }; for (int i = 0; i < 2; ++i) { std::cout << a[i] << ", "; } std::cout << '\n'; } 8
使用例 使用後 %:include <iostream> int main() <% int a<::> =
<% 114, 514, %>; for (int i = 0; i < 2; ++i) <% std::cout << a<:i:> << ", "; %> std::cout << '\n'; %> 9
使用例 使用後 %:include <iostream> int main() <% int a<::> =
<% 114, 514, %>; for (int i = 0; i < 2; ++i) <% std::cout << a<:i:> << ", "; %> std::cout << '\n'; %> 10 読みやすい! (異論は認める)
使用例 …これ、使う気がしない… 11
使用例 …これ、使う気がしない… と思いましたね? 12
使用例 使用前 template <class InputIterator, class Predicate> bool all_of(InputIterator first,
InputIterator last, Predicate pred) { for (; first != last; ++first) if (!pred(*first)) return false; return true; } 13 否定演算子が 見づらい!
使用例 使用後 template <class InputIterator, class Predicate> bool all_of(InputIterator first,
InputIterator last, Predicate pred) { for (; first != last; ++first) if (not pred(*first)) return false; return true; } 14 否定演算子が 見やすい! (異論は認めない)
使用例 どうですか? 使いたくなってきませんか? 15
ビット反転演算子が 見づらい! 使用例 使用前 const result_type Yp = (x[i] &
~mask) | (x[j] & mask); const size_t k = (i + m) % n; x[i] = x[k] ^ rshift<1>(Yp) ^ (a * (Yp & 1)); result_type z = x[i] ^ (rshift<u>(x[i]) & d); i = j; z ^= lshift<s>(z) & b; z ^= lshift<t>(z) & c; return z ^ rshift<l>(z); 16
ビット反転演算子が 見やすい! (異論は認める) 使用例 使用後 const result_type Yp = (x[i]
& compl mask) | (x[j] & mask); const size_t k = (i + m) % n; x[i] = x[k] ^ rshift<1>(Yp) ^ (a * (Yp & 1)); result_type z = x[i] ^ (rshift<u>(x[i]) & d); i = j; z ^= lshift<s>(z) & b; z ^= lshift<t>(z) & c; return z ^ rshift<l>(z); 17
使用例 使用後(過激派) const result_type Yp = (x<:i:> bitand compl mask)
bitor (x<:j:> bitand mask); const size_t k = (i + m) % n; X<:i:> = x<:k:> xor rshift<1>(Yp) xor (a * (Yp bitand 1)); result_type z = x<:i:> xor (rshift<u>(x<:i:>) bitand d); i = j; z xor_eq lshift<s>(z) bitand b; z xor_eq lshift<t>(z) bitand c; return z xor rshift<l>(z); 18
何もかもが 見やすい! (異論は認める) 使用例 使用後(過激派) const result_type Yp = (x<:i:>
bitand compl mask) bitor (x<:j:> bitand mask); const size_t k = (i + m) % n; X<:i:> = x<:k:> xor rshift<1>(Yp) xor (a * (Yp bitand 1)); result_type z = x<:i:> xor (rshift<u>(x<:i:>) bitand d); i = j; z xor_eq lshift<s>(z) bitand b; z xor_eq lshift<t>(z) bitand c; return z xor rshift<l>(z); 19
使用例 使用前 template <class InputIterator1, class InputIterator2, class BinaryPredicate> pair<InputIterator1,
InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred) { for (; first1 != last1 && first2 != last2; ++first1, ++first2) if (!pred(*first1, *first2)) break; return pair<InputIterator1, InputIterator2>(first1, first2); } 20
使用例 使用後 template <class InputIterator1, class InputIterator2, class BinaryPredicate> pair<InputIterator1,
InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred) { for (; first1 != last1 and first2 != last2; ++first1, ++first2) if (not pred(*first1, *first2)) break; return pair<InputIterator1, InputIterator2>(first1, first2); } 21
使用例 ところで皆さん、 代替トークンの字面に 惑わされてはいませんか? 22
使用例 使用前 struct S { S() = default; S(const S&)
= default; S(S&&) = default; ~S() = default; }; 23 まっ、まさか…
使用例 使用後 struct S <% S() = default; S(const S
bitand) = default; S(S and) = default; compl S() = default; %>; 24 まっ、まさか… そのまさかだ、フハ ハハハハハハハ!
使用例 使用後 struct S <% S() = default; S(const S
bitand) = default; S(S and) = default; compl S() = default; %>; 25 まっ、まさか… そのまさかだ、フハ ハハハハハハハ! スゴい! 参照もデストラクタも 見やすい!
まっ、まさか… そのまさかだ、フハ ハハハハハハハ! 使用例 使用後 struct S <% S() =
default; S(const S bitand) = default; S(S and) = default; compl S() = default; %>; 26 スゴい! 参照もデストラクタも 見やすい! ダメ!ゼッタイ!
結論 27
結論 代替トークン普及の会からのお知らせ みんな積極的に 代替トークンを使おう! せめて忘れないであげてください!!! 28
蛇足① ちなみに、C++17で廃止されてしまったトライグラフは、トー クン化どころかプリプロセス前に処理されていたので、文字 列内とかでも問答無用で変換対象になっていた。 29 代替 本来 代替 本来 代替
本来 ??= # ??( [ ??< { ??/ \ ??) ] ??> } ??’ ^ ??! | ??- ~
蛇足① 使用前(再掲) #include <iostream> int main() { int a[] =
{ 114, 514, }; for (int i = 0; i < 2; ++i) { std::cout << a[i] << ", "; } std::cout << '\n'; } 30
蛇足① 使用後(トライグラフ) ??=include <iostream> int main() ??< int a??(??) =
??< 114, 514, ??>; for (int i = 0; i < 2; ++i) ??< std::cout << a??(i??) << ", "; ??> std::cout << '\n'; ??> 31
蛇足① 使用後(トライグラフ) ??=include <iostream> int main() ??< int a??(??) =
??< 114, 514, ??>; for (int i = 0; i < 2; ++i) ??< std::cout << a??(i??) << ", "; ??> std::cout << '\n'; ??> 実際某青いI社のメインフレームで見たときは正直キモかった…(個人の感想です) 32 読みづらい! (異論は認める)
蛇足➁ C++のトークン化は、通常最長のトークンを切り出すことに なっている。 例) external ⇒ “extern”と“al”と言う2つのトークンではなく、 “external”と言う単一のトークン a+++++b ⇒
“a” “++” “+” “++” “b” ではなく、“a” “++” “++” “+” “b” と言うトークン列 33
蛇足➁ しかし、代替トークンに関連した以下のような例外がある。 “<::”の後ろに続く文字が“>”でも“:”でもなかった場合、 “<”と“::”と言う2つのトークンになる。 “<:” ⇒ “{” “<::” ⇒ “<”
と “::” “<::>” ⇒ “{” と “}” “<:::” ⇒ “{” と “::” まぁ普通この規則に引っかかることはおそらく一生のうち1度も無いとは思う… 34
蛇足➂ C言語では、キーワード型の代替トークンは言語仕様ではな く、ヘッダで提供されている。 35 代替 本来 代替 本来 代替 本来
<% { and && xor ^ %> } bitand & xor_eq ^= <: [ and_eq &= not ! :> ] or || not_eq != %: # bitor | compl ~ %:%: ## or_eq |= 言語仕様で提供 ヘッダiso646.hで提供
みんな代替トークン使とる。 使てへんのお前だけ。 36 完 終 制作・著作 ━━━━━ ⓃⒽⓀ