0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

25.7 Range access [iterator.range] C++N4910:2022 (649) p1025.cpp

Last updated at Posted at 2022-08-11

はじめに(Introduction)

N4910 Working Draft, Standard for Programming Language C++

n4910は、ISO/IEC JTC1 SC22 WG21の作業原案(Working Draft)です。
公式のISO/IEC 14882原本ではありません。
ISO/IEC JTC1 SC22 WG21では、可能な限り作業文書を公開し、幅広い意見を求めています。
一連の記事はコード断片をコンパイルできる形にする方法を検討してコンパイル、リンク、実行して、規格案の原文と処理系(g++, Clang++)との違いを確認し、技術内容を検討し、ISO/IEC JTC1 SC22 WG21にフィードバックするために用います。
また、CERT C++, MISRA C++等のコーディング標準のコード断片をコンパイルする際の参考にさせていただこうと考えています。CERT C++, MISRA C++が標準化の動きとの時間的なずれがあれば確認できれば幸いです。また、boostライブラリとの関連、Linux OS, TOPPERSカーネル、g++(GCC), clang++(LLVM)との関係も調査中です。
何か、抜け漏れ、耳より情報がありましたらおしらせくださると幸いです。

<この項は書きかけです。順次追記します。>

背景(back ground)

C/C++でコンパイルエラーが出ると、途方にくれることがしばしばあります。
何回かに1回は、該当するエラーが検索できます。
ただ、条件が違っていて、そこでの修正方法では目的を達成しないこともしばしばです。いろいろな条件のコンパイルエラーとその対応方法について、広く記録することによって、いつか同じエラーに遭遇した時にやくに立つことを目指しています。

この半年の間で、三度、自分のネットでの記録に助けられたことがあります。
また過去に解決できなかった記録を10種類以上、最近になって解決できたことがあります。それは、主に次の3つの情報に基づいています。

cpprefjp - C++日本語リファレンス

コンパイラの実装状況

また
https://researchmap.jp/joub9b3my-1797580/#_1797580
に記載したサイトのお世話になっています。

作業方針(sequence)

Clang++では-std=c++03, C++2bの2種類
g++では-std=c++03, c++2bの2種類
でコンパイルし、

1)コンパイルエラーを収集する。
2)コンパイルエラーをなくす方法を検討する。
コンパイルエラーになる例を示すだけが目的のコードは、コンパイルエラーをなくすのではなく、コンパイルエラーの種類を収集するだけにする。
文法を示すのが目的のコード場合に、コンパイルエラーをなくすのに手間がかかる場合は、順次作業します。
3)リンクエラーをなくす方法を検討する。
文法を示すのが目的のコード場合に、リンクエラーをなくすのに手間がかかる場合は、順次作業します。
4)意味のある出力を作る。
コンパイル、リンクが通っても、意味のある出力を示そうとすると、コンパイル・リンクエラーが出て収拾できそうにない場合がある。順次作業します。

1)だけのものから4)まで進んだものと色々ある状態です。一歩でも前に進むご助言をお待ちしています。「検討事項」の欄に現状を記録するようにしています。

C++N4910:2022 Standard Working Draft on ISO/IEC 14882(0) sample code compile list

C++N4741, 2018 Standard Working Draft on ISO/IEC 14882 sample code compile list

C++N4606, 2016符号断片編纂一覧(example code compile list)

C++N4606, 2016 Working Draft 2016, ISO/IEC 14882, C++ standard(1) Example code compile list
https://qiita.com/kaizen_nagoya/items/df5d62c35bd6ed1c3d43/

C++N3242, 2011 sample code compile list on clang++ and g++

編纂器(Compiler)

clang++ --version

Debian clang version 14.0.5-++20220610033153+c12386ae247c-1~exp1~20220610153237.151
Target: x86_64-pc-linux-gnu, Thread model: posix, InstalledDir: /usr/bin

g++- --version

g++ (GCC) 12.1.0 Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

25.7 Range access [iterator.range] C++N4910:2022 (649) p1025cpp

算譜(source code)

p1025.cpp
// C++N4910 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/n4910.pdf
const char * n4910 = " 25.7 Range access [iterator.range] C++N4910:2022 (649) p1025cpp";
// Debian clang version 14.0.5-++20220610033153+c12386ae247c-
// g++ (GCC) 12.1.0 Copyright (C) 2022 Free Software Foundation, Inc.
// Edited by Dr. OGAWA Kiyoshi. Compile procedure and results record.
// C++N4910:2022 Standard Working Draft on ISO/IEC 14882(0) sample code compile list
// https://qiita.com/kaizen_nagoya/items/fc957ddddd402004bb91

#include "N4910.h"

using namespace std;

// In addition to being available via inclusion of the <iterator> header, the function templates in 25.7 are available when any of the following headers are included: <array> (24.3.2), <deque> (24.3.3), <forward_list> (24.3.4), <list> (24.3.5), <map> (24.4.2), <regex> (32.3), <set> (24.4.3), <span> (24.7.2), <string> (23.4.2), <string_view> (23.3.2), <unordered_map> (24.5.2), <unordered_set> (24.5.3), and <vector> (24.3.6). Each of these templates is a designated customization point (16.4.5.2.1). template<class C> constexpr auto begin(C& c) -> decltype(c.begin());
template<class C> constexpr auto begin(const C& c) -> decltype(c.begin());
// Returns: c.begin().
template<class C> constexpr auto end(C& c) -> decltype(c.end());
template<class C> constexpr auto end(const C& c) -> decltype(c.end());
// Returns: c.end().
template<class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
// Returns: array.
template<class T, size_t N> constexpr T* end(T (&array)[N]) noexcept;
// Returns: array + N.
template<class C> constexpr auto cbegin(const C& c) noexcept(noexcept(std::begin(c)))
-> decltype(std::begin(c));
// Returns: std::begin(c).
template<class C> constexpr auto cend(const C& c) noexcept(noexcept(std::end(c)))
-> decltype(std::end(c));
// Returns: std::end(c).
template<class C> constexpr auto rbegin(C& c) -> decltype(c.rbegin());
template<class C> constexpr auto rbegin(const C& c) -> decltype(c.rbegin());
// Returns: c.rbegin().
template<class C> constexpr auto rend(C& c) -> decltype(c.rend());
template<class C> constexpr auto rend(const C& c) -> decltype(c.rend());
// Returns: c.rend().
template<class T, size_t N> constexpr reverse_iterator<T*> rbegin(T (&array)[N]);
// Returns: reverse_iterator<T*>(array + N).
template<class T, size_t N> constexpr reverse_iterator<T*> rend(T (&array)[N]);
// Returns: reverse_iterator<T*>(array).
template<class E> constexpr reverse_iterator<const E*> rbegin(initializer_list<E> il);
// Returns: reverse_iterator<const E*>(il.end()).
template<class E> constexpr reverse_iterator<const E*> rend(initializer_list<E> il);
// Returns: reverse_iterator<const E*>(il.begin()).
template<class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c));
// Returns: std::rbegin(c).
template<class C> constexpr auto crend(const C& c) -> decltype(std::rend(c));
// Returns: std::rend(c).
template<class C> constexpr auto size(const C& c) -> decltype(c.size());
// Returns: c.size().
template<class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept;
// Returns: N.
template<class C> constexpr auto ssize(const C& c)
-> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>;
// Effects: Equivalent to:
// return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>>(c.size());
// p1025.cpp:55:1: error: expected unqualified-id before 'return'
//   55 | return static_cast<common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>>(c.size());
//      | ^~~~~~
template<class T, ptrdiff_t N> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept;
// Returns: N.
template<class C> [[nodiscard]] constexpr auto empty(const C& c) -> decltype(c.empty());
// Returns: c.empty().
template<class T, size_t N> [[nodiscard]] constexpr bool empty(const T (&array)[N]) noexcept;
// Returns: false.
template<class E> [[nodiscard]] constexpr bool empty(initializer_list<E> il) noexcept;
// Returns: il.size() == 0.
template<class C> constexpr auto data(C& c) -> decltype(c.data());
template<class C> constexpr auto data(const C& c) -> decltype(c.data());
// Returns: c.data().
template<class T, size_t N> constexpr T* data(T (&array)[N]) noexcept;
// Returns: array.
template<class E> constexpr const E* data(initializer_list<E> il) noexcept;
// Returns: il.begin().
int main() {
    cout  <<  n4910 << endl;
    return EXIT_SUCCESS;
}

編纂・実行結果(compile and go)

bash
$ clang++ p1025.cpp -std=03 -o p1025l -I. -Wall
In file included from p1025.cpp:10:
In file included from ./N4910.h:11:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/atomic:38:
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/c++0x_warning.h:32:2: error: This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support \
 ^
p1025.cpp:15:22: error: unknown type name 'constexpr'
   template<class C> constexpr auto begin(const C& c) -> decltype(c.begin());
                     ^
p1025.cpp:15:32: error: illegal storage class on function
   template<class C> constexpr auto begin(const C& c) -> decltype(c.begin());
                               ^
p1025.cpp:15:54: error: expected ';' at end of declaration
   template<class C> constexpr auto begin(const C& c) -> decltype(c.begin());
                                                     ^
                                                     ;
p1025.cpp:15:55: error: cannot use arrow operator on a type
   template<class C> constexpr auto begin(const C& c) -> decltype(c.begin());
                                                      ^
p1025.cpp:17:19: error: unknown type name 'constexpr'
template<class C> constexpr auto end(C& c) -> decltype(c.end());
                  ^
p1025.cpp:17:29: error: illegal storage class on function
template<class C> constexpr auto end(C& c) -> decltype(c.end());
                            ^
p1025.cpp:17:43: error: expected ';' at end of declaration
template<class C> constexpr auto end(C& c) -> decltype(c.end());
                                          ^
                                          ;
p1025.cpp:17:44: error: cannot use arrow operator on a type
template<class C> constexpr auto end(C& c) -> decltype(c.end());
                                           ^
p1025.cpp:18:22: error: unknown type name 'constexpr'
   template<class C> constexpr auto end(const C& c) -> decltype(c.end());
                     ^
p1025.cpp:18:32: error: illegal storage class on function
   template<class C> constexpr auto end(const C& c) -> decltype(c.end());
                               ^
p1025.cpp:18:52: error: expected ';' at end of declaration
   template<class C> constexpr auto end(const C& c) -> decltype(c.end());
                                                   ^
                                                   ;
p1025.cpp:18:53: error: cannot use arrow operator on a type
   template<class C> constexpr auto end(const C& c) -> decltype(c.end());
                                                    ^
p1025.cpp:20:29: error: unknown type name 'constexpr'
template<class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
                            ^
p1025.cpp:20:39: warning: variable templates are a C++14 extension [-Wc++14-extensions]
template<class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
                                      ^
p1025.cpp:20:40: error: expected ';' at end of declaration
template<class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
                                       ^
                                       ;
p1025.cpp:20:42: error: C++ requires a type specifier for all declarations
template<class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
                                         ^
p1025.cpp:20:52: error: use of undeclared identifier 'array'
template<class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
                                                   ^
p1025.cpp:20:59: error: use of undeclared identifier 'N'
template<class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
                                                          ^
p1025.cpp:20:62: error: expected ';' after top level declarator
template<class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
                                                             ^
                                                             ;
fatal error: too many errors emitted, stopping now [-ferror-limit=]
1 warning and 20 errors generated.
$ clang++ p1025.cpp -std=2b -o p1025l -I. -Wall
 25.7 Range access [iterator.range] C++N4910:2022 (649) p1025cpp

$ g++ p1025.cpp -std=03 -o p1025g -I. -Wall
In file included from /usr/local/include/c++/12.1.0/atomic:38,
                 from N4910.h:11,
                 from p1025.cpp:10:
/usr/local/include/c++/12.1.0/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
   32 | #error This file requires compiler and library support \
      |  ^~~~~
p1025.cpp:15:22: warning: identifier 'constexpr' is a keyword in C++11 [-Wc++11-compat]
   15 |    template<class C> constexpr auto begin(const C& c) -> decltype(c.begin());
      |                      ^~~~~~~~~
p1025.cpp:15:58: warning: identifier 'decltype' is a keyword in C++11 [-Wc++11-compat]
   15 |    template<class C> constexpr auto begin(const C& c) -> decltype(c.begin());
      |                                                          ^~~~~~~~
p1025.cpp:20:63: warning: identifier 'noexcept' is a keyword in C++11 [-Wc++11-compat]
   20 | template<class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
      |                                                               ^~~~~~~~
p1025.cpp:15:22: error: 'constexpr' does not name a type
   15 |    template<class C> constexpr auto begin(const C& c) -> decltype(c.begin());
      |                      ^~~~~~~~~
p1025.cpp:15:22: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:17:19: error: 'constexpr' does not name a type
   17 | template<class C> constexpr auto end(C& c) -> decltype(c.end());
      |                   ^~~~~~~~~
p1025.cpp:17:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:18:22: error: 'constexpr' does not name a type
   18 |    template<class C> constexpr auto end(const C& c) -> decltype(c.end());
      |                      ^~~~~~~~~
p1025.cpp:18:22: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:20:29: error: 'constexpr' does not name a type
   20 | template<class T, size_t N> constexpr T* begin(T (&array)[N]) noexcept;
      |                             ^~~~~~~~~
p1025.cpp:20:29: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:22:29: error: 'constexpr' does not name a type
   22 | template<class T, size_t N> constexpr T* end(T (&array)[N]) noexcept;
      |                             ^~~~~~~~~
p1025.cpp:22:29: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:24:19: error: 'constexpr' does not name a type
   24 | template<class C> constexpr auto cbegin(const C& c) noexcept(noexcept(std::begin(c)))
      |                   ^~~~~~~~~
p1025.cpp:24:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:27:19: error: 'constexpr' does not name a type
   27 | template<class C> constexpr auto cend(const C& c) noexcept(noexcept(std::end(c)))
      |                   ^~~~~~~~~
p1025.cpp:27:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:30:19: error: 'constexpr' does not name a type
   30 | template<class C> constexpr auto rbegin(C& c) -> decltype(c.rbegin());
      |                   ^~~~~~~~~
p1025.cpp:30:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:31:19: error: 'constexpr' does not name a type
   31 | template<class C> constexpr auto rbegin(const C& c) -> decltype(c.rbegin());
      |                   ^~~~~~~~~
p1025.cpp:31:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:33:19: error: 'constexpr' does not name a type
   33 | template<class C> constexpr auto rend(C& c) -> decltype(c.rend());
      |                   ^~~~~~~~~
p1025.cpp:33:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:34:19: error: 'constexpr' does not name a type
   34 | template<class C> constexpr auto rend(const C& c) -> decltype(c.rend());
      |                   ^~~~~~~~~
p1025.cpp:34:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:36:29: error: 'constexpr' does not name a type
   36 | template<class T, size_t N> constexpr reverse_iterator<T*> rbegin(T (&array)[N]);
      |                             ^~~~~~~~~
p1025.cpp:36:29: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:38:29: error: 'constexpr' does not name a type
   38 | template<class T, size_t N> constexpr reverse_iterator<T*> rend(T (&array)[N]);
      |                             ^~~~~~~~~
p1025.cpp:38:29: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:40:19: error: 'constexpr' does not name a type
   40 | template<class E> constexpr reverse_iterator<const E*> rbegin(initializer_list<E> il);
      |                   ^~~~~~~~~
p1025.cpp:40:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:42:19: error: 'constexpr' does not name a type
   42 | template<class E> constexpr reverse_iterator<const E*> rend(initializer_list<E> il);
      |                   ^~~~~~~~~
p1025.cpp:42:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:44:19: error: 'constexpr' does not name a type
   44 | template<class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c));
      |                   ^~~~~~~~~
p1025.cpp:44:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:46:19: error: 'constexpr' does not name a type
   46 | template<class C> constexpr auto crend(const C& c) -> decltype(std::rend(c));
      |                   ^~~~~~~~~
p1025.cpp:46:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:48:19: error: 'constexpr' does not name a type
   48 | template<class C> constexpr auto size(const C& c) -> decltype(c.size());
      |                   ^~~~~~~~~
p1025.cpp:48:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:50:29: error: 'constexpr' does not name a type
   50 | template<class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept;
      |                             ^~~~~~~~~
p1025.cpp:50:29: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:52:19: error: 'constexpr' does not name a type
   52 | template<class C> constexpr auto ssize(const C& c)
      |                   ^~~~~~~~~
p1025.cpp:52:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:59:32: error: 'constexpr' does not name a type
   59 | template<class T, ptrdiff_t N> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept;
      |                                ^~~~~~~~~
p1025.cpp:59:32: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:61:19: error: expected unqualified-id before '[' token
   61 | template<class C> [[nodiscard]] constexpr auto empty(const C& c) -> decltype(c.empty());
      |                   ^
p1025.cpp:63:29: error: expected unqualified-id before '[' token
   63 | template<class T, size_t N> [[nodiscard]] constexpr bool empty(const T (&array)[N]) noexcept;
      |                             ^
p1025.cpp:65:19: error: expected unqualified-id before '[' token
   65 | template<class E> [[nodiscard]] constexpr bool empty(initializer_list<E> il) noexcept;
      |                   ^
p1025.cpp:67:19: error: 'constexpr' does not name a type
   67 | template<class C> constexpr auto data(C& c) -> decltype(c.data());
      |                   ^~~~~~~~~
p1025.cpp:67:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:68:19: error: 'constexpr' does not name a type
   68 | template<class C> constexpr auto data(const C& c) -> decltype(c.data());
      |                   ^~~~~~~~~
p1025.cpp:68:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:70:29: error: 'constexpr' does not name a type
   70 | template<class T, size_t N> constexpr T* data(T (&array)[N]) noexcept;
      |                             ^~~~~~~~~
p1025.cpp:70:29: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1025.cpp:72:19: error: 'constexpr' does not name a type
   72 | template<class E> constexpr const E* data(initializer_list<E> il) noexcept;
      |                   ^~~~~~~~~
p1025.cpp:72:19: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'

$ g++ p1025.cpp -std=2b -o p1025g -I. -Wall
 25.7 Range access [iterator.range] C++N4910:2022 (649) p1025cpp

検討事項(agenda)

コンパイルエラーを取るか、コンパイルエラーの理由を解説する。

参考資料(reference)

関連する自己参照以外は、こちらの先頭に移転。

C言語(C++)に対する誤解、曲解、無理解、爽快。

#include "N4910.h"

C++N4910資料の改善点

dockerにclang

docker gnu(gcc/g++) and llvm(clang/clang++)

コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)

C++N4910:2022 tag follower 300人超えました。ありがとうございます。

astyle 使ってみた

DoCAP(ドゥーキャップ)って何ですか?

小川メソッド 覚え(書きかけ)

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>

文書履歴(document history)

ver. 0.01 初稿  20220811

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?