summaryrefslogtreecommitdiffhomepage
path: root/services/blog/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'services/blog/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html')
-rw-r--r--services/blog/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html193
1 files changed, 193 insertions, 0 deletions
diff --git a/services/blog/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html b/services/blog/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html
new file mode 100644
index 00000000..53bd4c70
--- /dev/null
+++ b/services/blog/public/posts/2021-10-02/cpp-you-can-use-keywords-in-attributes/index.html
@@ -0,0 +1,193 @@
+<!DOCTYPE html>
+<html lang="ja-JP">
+ <head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta name="author" content="nsfisis">
+ <meta name="copyright" content="&copy; 2021 nsfisis">
+ <meta name="description" content="C++ の属性構文の属性名には、キーワードが使える。ネタ記事。">
+ <meta name="keywords" content="C++,C++ 17">
+ <meta property="og:type" content="article">
+ <meta property="og:title" content="【C++】 属性構文の属性名にはキーワードが使える|REPL: Rest-Eat-Program Loop">
+ <meta property="og:description" content="C++ の属性構文の属性名には、キーワードが使える。ネタ記事。">
+ <meta property="og:site_name" content="REPL: Rest-Eat-Program Loop">
+ <meta property="og:locale" content="ja_JP">
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
+ <title>【C++】 属性構文の属性名にはキーワードが使える|REPL: Rest-Eat-Program Loop</title>
+ <link rel="stylesheet" href="/style.css?h=9513229b52eb2041b99ba1b959305633">
+ </head>
+ <body class="single">
+ <header class="header">
+ <div class="site-logo">
+ <a href="/">REPL: Rest-Eat-Program Loop</a>
+ </div>
+ <nav class="nav">
+ <ul>
+ <li>
+ <a href="/about/">About</a>
+ </li>
+ <li>
+ <a href="/posts/">Posts</a>
+ </li>
+ <li>
+ <a href="/slides/">Slides</a>
+ </li>
+ <li>
+ <a href="/tags/">Tags</a>
+ </li>
+ </ul>
+ </nav>
+ </header>
+ <main class="main">
+ <article class="post-single">
+ <header class="post-header">
+ <h1 class="post-title">【C++】 属性構文の属性名にはキーワードが使える</h1>
+ <ul class="post-tags">
+ <li class="tag">
+ <a href="/tags/cpp/">C++</a>
+ </li>
+ <li class="tag">
+ <a href="/tags/cpp17/">C++ 17</a>
+ </li>
+ </ul>
+ </header>
+ <div class="post-content">
+ <section id="changelog">
+ <h2><a href="#changelog">更新履歴</a></h2>
+ <ol>
+ <li class="revision">
+ <time datetime="2021-10-02">2021-10-02</time>: Qiita から移植
+ </li>
+ </ol>
+ </section>
+ <div class="admonition">
+ <div class="admonition-label">
+ NOTE
+ </div>
+ <div class="admonition-content">
+ <p>
+ この記事は Qiita から移植してきたものです。元 URL: <a href="https://qiita.com/nsfisis/items/94090937bcf860cfa93b" rel="noreferrer" target="_blank">https://qiita.com/nsfisis/items/94090937bcf860cfa93b</a>
+ </p>
+ </div>
+ </div>
+ <p>
+ タイトル落ち。まずはこのコードを見て欲しい。
+ </p>
+ <div class="codeblock">
+ <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#D73A49">#include</span><span style="color:#032F62"> &#x3C;iostream></span></span>
+<span class="line"></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">alignas</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">alignof</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">and</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">and_eq</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">asm</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">auto</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">bitand</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">bitor</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">bool</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">break</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">case</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">catch</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">char</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">char16_t</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">char32_t</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">class</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">compl</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">const</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">const_cast</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">constexpr</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">continue</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">decltype</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">default</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">delete</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">do</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">double</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">dynamic_cast</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">else</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">enum</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">explicit</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">export</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">extern</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">false</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">final</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">float</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">for</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">friend</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">goto</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">if</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">inline</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">int</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">long</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">mutable</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">namespace</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">new</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">noexcept</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">not</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">not_eq</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">nullptr</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">operator</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">or</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">or_eq</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">override</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">private</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">protected</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">public</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">register</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">reinterpret_cast</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">return</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">short</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">signed</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">sizeof</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">static</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">static_assert</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">static_cast</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">struct</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">switch</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">template</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">this</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">thread_local</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">throw</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">true</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">try</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">typedef</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">typeid</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">typename</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">union</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">unsigned</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#6F42C1">virtual</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">void</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">volatile</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">wchar_t</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">while</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">xor</span><span style="color:#005CC5">]]</span><span style="color:#005CC5"> [[</span><span style="color:#6F42C1">xor_eq</span><span style="color:#005CC5">]]</span></span>
+<span class="line"><span style="color:#6A737D">// [[using]]</span></span>
+<span class="line"><span style="color:#D73A49">int</span><span style="color:#6F42C1"> main</span><span style="color:#24292E">() {</span></span>
+<span class="line"><span style="color:#6F42C1"> std</span><span style="color:#24292E">::cout </span><span style="color:#D73A49">&#x3C;&#x3C;</span><span style="color:#032F62"> "Hello, World!"</span><span style="color:#D73A49"> &#x3C;&#x3C;</span><span style="color:#6F42C1"> std</span><span style="color:#24292E">::endl;</span></span>
+<span class="line"><span style="color:#24292E">}</span></span></code></pre>
+ </div>
+ <p>
+ コンパイラのバージョン
+ </p>
+ <div class="codeblock">
+ <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span>$ clang++ –version Apple clang version 11.0.0</span></span>
+<span class="line"><span>(clang-1100.0.33.8) Target: x86_64-apple-darwin19.6.0 Thread model:</span></span>
+<span class="line"><span>posix InstalledDir:</span></span>
+<span class="line"><span>/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin</span></span></code></pre>
+ </div>
+ <p>
+ コンパイルコマンド (C17指定)
+ </p>
+ <div class="codeblock">
+ <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span>$ clang –std=c++17 hoge.cpp</span></span></code></pre>
+ </div>
+ <p>
+ この記事から得られるものはこれ以上ないので以下は蛇足になる。
+ </p>
+ <p>
+ 別件で <a href="https://en.cppreference.com/w/cpp/language/identifiers" rel="noreferrer" target="_blank">cppreference.com の identifier のページ</a>を読んでいた時、次の文が目に止まった。
+ </p>
+ <blockquote>
+ <ul>
+ <li>
+ the identifiers that are keywords cannot be used for other purposes;
+ <ul>
+ <li>
+ The only place they can be used as non-keywords is in an attribute-token. (e.g. [[private]] is a valid attribute) (since C++11)
+ </li>
+ </ul>
+ </li>
+ </ul>
+ </blockquote>
+ <p>
+ キーワードでも属性として指定する場合は非キーワードとして使えるらしい。実際にやってみる。
+ </p>
+ <p>
+ 同サイトの <a href="https://en.cppreference.com/w/cpp/keyword" rel="noreferrer" target="_blank">keywords のページ</a> から一覧を拝借し、上のコードが出来上がった (C++17 においてキーワードでないものなど、一部省いている)。 大量の警告 (unknown attribute `〇〇’ ignored) がコンパイラから出力されるが、コンパイルできる。
+ </p>
+ <p>
+ 上のコードでは <code>[[using]]</code> をコメントアウトしているが、これは <code>using</code> キーワードのみ属性構文の中で意味を持つからであり、このコメントアウトを外すとコンパイルに失敗する。
+ </p>
+ <div class="codeblock">
+ <pre class="shiki github-light" style="background-color:#f5f5f5;color:#24292e" tabindex="0"><code><span class="line"><span style="color:#6A737D">// using の例</span></span>
+<span class="line"><span style="color:#005CC5">[[</span><span style="color:#D73A49">using</span><span style="color:#6F42C1"> foo</span><span style="color:#005CC5">: </span><span style="color:#6F42C1">attr1</span><span style="color:#005CC5">, </span><span style="color:#6F42C1">attr2</span><span style="color:#005CC5">]]</span><span style="color:#D73A49"> int</span><span style="color:#24292E"> x;</span><span style="color:#6A737D"> // [[foo::attr1, foo::attr2]] の糖衣構文</span></span></code></pre>
+ </div>
+ <p>
+ C++17 の仕様も見てみる (正確には標準化前のドラフト)。
+ </p>
+ <p>
+ 引用元: <a href="https://timsong-cpp.github.io/cppwp/n4659/dcl.attr#grammar-4" rel="noreferrer" target="_blank">https://timsong-cpp.github.io/cppwp/n4659/dcl.attr#grammar-4</a>
+ </p>
+ <blockquote>
+ <p>
+ If a keyword or an alternative token that satisfies the syntactic requirements of an identifier is contained in an attribute-token, it is considered an identifier.
+ </p>
+ </blockquote>
+ <p>
+ 「<code>identifier</code> の構文上の要件を満たすキーワードまたは代替トークンが <code>attribute-token</code> に含まれている場合、<code>identifier</code> とみなされる」とある。どうやら間違いないようだ。
+ </p>
+ <p>
+ ところで、代替トークン (alternative token) とは <code>and</code> (<code>&amp;</code>) や <code>bitor</code> (<code>|</code>) などのことだが、<code>identifier</code> の構文上の要件を満たさないような代替トークンなどあるのか? 疑問に思って調べたところ、代替トークンという語にはダイグラフも含まれるらしい (参考: <a href="https://timsong-cpp.github.io/cppwp/n4659/lex.digraph" rel="noreferrer" target="_blank">同ドラフト</a> )
+ </p>
+ <ul>
+ <li>
+ <code>&lt;%</code> → <code>{</code>
+ </li>
+ <li>
+ <code>%&gt;</code> → <code>}</code>
+ </li>
+ <li>
+ <code>&lt;:</code> → <code>[</code>
+ </li>
+ <li>
+ <code>:&gt;</code> → <code>]</code>
+ </li>
+ <li>
+ <code>%:</code> → <code>#</code>
+ </li>
+ <li>
+ <code>%:%:</code> → <code>##</code>
+ </li>
+ </ul>
+ <p>
+ 「<code>identifier</code> の構文上の要件を満たさないような代替トークン」はこれらが当てはまると思われる。
+ </p>
+ <p>
+ 調べた感想: 字句解析器か構文解析器が辛そう
+ </p>
+ </div>
+ </article>
+ </main>
+ <footer class="footer">
+ &copy; 2021 nsfisis
+ </footer>
+ </body>
+</html>