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
470
C++14 の新機能 ライブラリ編 / new features of C++14 - Library
kariyamitsuru
0
770
Other Decks in Programming
See All in Programming
軽量Java基盤の設計 DIコンテナに頼らない、長期保守と1秒起動の実現 JJUG CCC 2026 Spring
macha64
0
480
dRuby over BLE
makicamel
2
330
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.2k
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
230
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
120
運用エージェントは "作る" から "育てる" へ - 記憶と自己進化の3層設計パターン / self-evolving-agents-three-layer-agent-design
gawa
12
3.6k
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
170
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.3k
Contextとはなにか
chiroruxx
0
250
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
560
New "Type" system on PicoRuby
pocke
1
780
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.3k
Featured
See All Featured
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
610
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2k
Testing 201, or: Great Expectations
jmmastey
46
8.2k
Bash Introduction
62gerente
615
210k
Building Adaptive Systems
keathley
44
3k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
410
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
190
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
290
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Design in an AI World
tapps
1
230
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 完 終 制作・著作 ━━━━━ ⓃⒽⓀ