LoginSignup
0
0

More than 1 year has passed since last update.

29.4 Time-related traits [time.traits]C++N4910:2022 (672) p1370.cpp

Posted at

はじめに(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.

29.4 Time-related traits [time.traits]C++N4910:2022 (672) p1370.cpp

算譜(source code)

p1370.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 = "29.4 Time-related traits  [time.traits]C++N4910:2022 (672) p1370.cpp";
// 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;

// 29.4.1 treat_as_floating_point [time.traits.is.fp]
template<class Rep> struct treat_as_floating_point : is_floating_point<Rep> { };
//  The duration template uses the treat_as_floating_point trait to help determine if a duration object can be converted to another duration with a different tick period. If treat_as_floating_point_v<Rep> is true, then implicit conversions are allowed among durations. Otherwise, the implicit convertibility depends on the tick periods of the durations.
// [Note 1: The intention of this trait is to indicate whether a given class behaves like a floating-point type, and thus allows division of one value by another with acceptable loss of precision. If treat_as_floating_point_v<Rep> is false, Rep will be treated as if it behaved like an integral type for the purpose of these conversions.
// 29.4.2 duration_values [time.traits.duration.values]
template<class Rep>
struct duration_values {
public:
    static constexpr Rep zero() noexcept;
    static constexpr Rep min() noexcept;
    static constexpr Rep max() noexcept;
};
//  The duration template uses the duration_values trait to construct special values of the duration’s representation (Rep). This is done because the representation can be a class type with behavior that requires some other implementation to return these special values. In that case, the author of that class type should specialize duration_values to return the indicated values.
static constexpr Rep zero() noexcept;
// Returns: Rep(0).
// [Note 1: Rep(0) is specified instead of Rep() because Rep() can have some other meaning, such as an uninitialized value.
// Remarks: The value returned shall be the additive identity.
// The period of the duration indicated by this specialization of common_type is the greatest common divisor of Period1 and Period2.
// [Note 1: This can be computed by forming a ratio of the greatest common divisor of Period1::num and Period2::num and the least common multiple of Period1::den and Period2::den.
//  [Note 2: The typedef name type is a synonym for the duration with the largest tick period possible where both duration arguments will convert to it without requiring a division operation. The representation of this type is intended to be able to hold any value resulting from this conversion with no truncation error, although floating-point durations can have round-off errors.
template<class Clock, class Duration1, class Duration2>
struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
    using type = chrono::time_point<Clock, common_type_t<Duration1, Duration2>>;
    static constexpr Rep min() noexcept;
// Returns: numeric_limits<Rep>::lowest().
// Remarks: The value returned shall compare less than or equal to zero().
    static constexpr Rep max() noexcept;
// Returns: numeric_limits<Rep>::max().
// Remarks: The value returned shall compare greater than zero().
// 29.4.3 Specializations of common_type [time.traits.specializations]
    template<class Rep1, class Period1, class Rep2, class Period2>
    struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>> {
        using type = chrono::duration<common_type_t<Rep1, Rep2>, see below>;
    };
};
//  The common type of two time_point types is a time_point with the same clock as the two types and the common type of their two durations.
// 29.4.4 Class template is_clock [time.traits.is.clock]
template<class T> struct is_clock;
//  is_clock is a Cpp17UnaryTypeTrait (21.3.2) with a base characteristic of true_type if T meets the Cpp17Clock requirements (29.3), otherwise false_type. For the purposes of the specification of this trait, the extent to which an implementation determines that a type cannot meet the Cpp17Clock requirements is unspecified, except that as a minimum a type T shall not qualify as a Cpp17Clock unless it meets all of the following conditions:
// — the qualified-ids T::rep, T::period, T::duration, and T::time_point are valid and each denotes a type (13.10.3),
// — the expression T::is_steady is well-formed when treated as an unevaluated operand (7.2.3),
// — the expression T::now() is well-formed when treated as an unevaluated operand. 2 The behavior of a program that adds specializations for is_clock is undefined.
int main() {
    cout  <<  n4910 << endl;
    return EXIT_SUCCESS;
}

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

bash
$ clang++ p1370.cpp -std=03 -o p1370l -I. -Wall
In file included from p1370.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 \
 ^
p1370.cpp:15:57: error: no template named 'is_floating_point'
   template<class Rep> struct treat_as_floating_point : is_floating_point<Rep> { };
                                                        ^
p1370.cpp:22:12: error: unknown type name 'constexpr'
    static constexpr Rep zero() noexcept;
           ^
p1370.cpp:22:25: error: expected ';' at end of declaration list
    static constexpr Rep zero() noexcept;
                        ^
                        ;
p1370.cpp:23:12: error: unknown type name 'constexpr'
    static constexpr Rep min() noexcept;
           ^
p1370.cpp:23:25: error: expected ';' at end of declaration list
    static constexpr Rep min() noexcept;
                        ^
                        ;
p1370.cpp:24:12: error: unknown type name 'constexpr'
    static constexpr Rep max() noexcept;
           ^
p1370.cpp:24:25: error: expected ';' at end of declaration list
    static constexpr Rep max() noexcept;
                        ^
                        ;
p1370.cpp:27:11: error: unknown type name 'constexpr'
   static constexpr Rep zero() noexcept;
          ^
p1370.cpp:27:24: error: expected ';' after top level declarator
   static constexpr Rep zero() noexcept;
                       ^
                       ;
p1370.cpp:35:25: error: use of undeclared identifier 'chrono'
     struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
                        ^
p1370.cpp:35:44: error: 'Clock' does not refer to a value
     struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
                                           ^
p1370.cpp:34:19: note: declared here
   template<class Clock, class Duration1, class Duration2>
                  ^
p1370.cpp:35:13: error: explicit specialization of undeclared template struct 'common_type'
     struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
            ^          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
p1370.cpp:35:61: error: expected unqualified-id
     struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
                                                            ^
14 errors generated.
$ clang++ p1370.cpp -std=2b -o p1370l -I. -Wall
p1370.cpp:27:21: error: unknown type name 'Rep'
   static constexpr Rep zero() noexcept;
                    ^
p1370.cpp:35:25: error: use of undeclared identifier 'chrono'
     struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
                        ^
p1370.cpp:35:44: error: 'Clock' does not refer to a value
     struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
                                           ^
p1370.cpp:34:19: note: declared here
   template<class Clock, class Duration1, class Duration2>
                  ^
p1370.cpp:35:61: error: expected unqualified-id
     struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
                                                            ^
4 errors generated.

$ g++ p1370.cpp -std=03 -o p1370g -I. -Wall
In file included from /usr/local/include/c++/12.1.0/atomic:38,
                 from N4910.h:11,
                 from p1370.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 \
      |  ^~~~~
p1370.cpp:22:12: warning: identifier 'constexpr' is a keyword in C++11 [-Wc++11-compat]
   22 |     static constexpr Rep zero() noexcept;
      |            ^~~~~~~~~
p1370.cpp:22:33: warning: identifier 'noexcept' is a keyword in C++11 [-Wc++11-compat]
   22 |     static constexpr Rep zero() noexcept;
      |                                 ^~~~~~~~
p1370.cpp:15:74: error: expected template-name before '<' token
   15 |    template<class Rep> struct treat_as_floating_point : is_floating_point<Rep> { };
      |                                                                          ^
p1370.cpp:15:74: error: expected '{' before '<' token
p1370.cpp:22:12: error: 'constexpr' does not name a type
   22 |     static constexpr Rep zero() noexcept;
      |            ^~~~~~~~~
p1370.cpp:22:12: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1370.cpp:23:12: error: 'constexpr' does not name a type
   23 |     static constexpr Rep min() noexcept;
      |            ^~~~~~~~~
p1370.cpp:23:12: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1370.cpp:24:12: error: 'constexpr' does not name a type
   24 |     static constexpr Rep max() noexcept;
      |            ^~~~~~~~~
p1370.cpp:24:12: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1370.cpp:27:11: error: 'constexpr' does not name a type
   27 |    static constexpr Rep zero() noexcept;
      |           ^~~~~~~~~
p1370.cpp:27:11: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1370.cpp:35:13: error: 'common_type' is not a class template
   35 |      struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
      |             ^~~~~~~~~~~
p1370.cpp:35:25: error: 'chrono' was not declared in this scope
   35 |      struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
      |                         ^~~~~~
p1370.cpp:36:14: error: expected nested-name-specifier before 'type'
   36 |        using type = chrono::time_point<Clock, common_type_t<Duration1, Duration2>>;
      |              ^~~~
p1370.cpp:37:8: error: 'constexpr' does not name a type
   37 | static constexpr Rep min() noexcept;
      |        ^~~~~~~~~
p1370.cpp:37:8: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1370.cpp:40:8: error: 'constexpr' does not name a type
   40 | static constexpr Rep max() noexcept;
      |        ^~~~~~~~~
p1370.cpp:40:8: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p1370.cpp:45:22: error: 'chrono' was not declared in this scope
   45 |   struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>> {
      |                      ^~~~~~
p1370.cpp:45:85: error: wrong number of template arguments (1, should be 3)
   45 |   struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>> {
      |                                                                                     ^~
p1370.cpp:35:98: note: provided for 'template<class Clock, class Duration1, class Duration2> struct common_type'
   35 |     struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
      |                                                                                                 ^~


$ g++ p1370.cpp -std=2b -o p1370g -I. -Wall
p1370.cpp:27:21: error: 'Rep' does not name a type
   27 |    static constexpr Rep zero() noexcept;
      |                     ^~~
p1370.cpp:35:25: error: 'chrono' was not declared in this scope; did you mean 'chroot'?
   35 |      struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
      |                         ^~~~~~
      |                         chroot
p1370.cpp:35:98: error: template argument 1 is invalid
   35 |     struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
      |                                                                                                 ^~


検討事項(agenda)

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

応用例1 AUTOSAR C++

AUTOSARでC++のコーディング標準を作っている。 
MISRA-C++コーディング標準の改訂をまたずに、C++14に対応したかったためかもしれない。 

Autosar Guidelines C++14 example code compile list

応用例2 MISRA C++

MISRA C++, AUTOSAR C++について

MISRA C++ 5-0-16

応用例3 CERT C++

MISRA C/C++, AUTOSAR C++, CERT C/C++とC/C++工業標準をコンパイルする

応用例4 箱庭 

箱庭ではUnityをはじめC++を使っているらしい。 

ここでコンパイルしたコードと同じようなコードを使っているか、
ここで出たコンパイルエラーと同じようなエラーがでたかを
いろいろな版のコンパイラでコンパイルして確認していく。

仮想戦略会議「箱庭」

お盆には「箱庭」記事を書きましょう「もくもく会」の題材になる(1)

お盆には「箱庭」記事を書きましょう「もくもく会」の題材になる(2)

参考資料(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 使ってみた

【個人開発】 効率的な背景 <エンジニア夏休み企画>

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

文書履歴(document history)

ver. 0.01 初稿  20220820

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