Skip to content

Latest commit

 

History

History
99 lines (75 loc) · 2.99 KB

op_constructor.md

File metadata and controls

99 lines (75 loc) · 2.99 KB

コンストラクタ

  • format[meta header]
  • function[meta id-type]
  • std[meta namespace]
  • basic_format_string[meta class]
  • cpp23[meta cpp]
template <class T>
consteval basic_format_string(const T& s); // (1) C++23

basic_format_string(runtime-format-string<charT> s) noexcept; // (2) C++26
  • runtime-format-string[link /reference/format/runtime-format-string.md]

概要

basic_format_stringオブジェクトを構築する。

  • (1) : コンパイル時の書式文字列を受け取る
  • (2) : 実行時の書式文字列を受け取る

テンプレートパラメータ制約

効果

メンバ変数としてbasic_string_view<charT> str;が定義されるとして、str(s)で初期化する。

備考

  • この関数の呼び出しは、strargsの書式文字列であるようなArgs型のargsが存在しない限り、コア定数式ではない
    • 意味 : 書式文字列が引数列argsと合わせて正しくなければ、定数式評価 (consteval) が実行できずコンパイルエラーとなる
    • 以下のようなコードはコンパイルエラーとなる:
    int main() {
      auto str = std::format("{:d}", "I am not a number"); // コンパイルエラー!型が合わない
    }
    • std::format[link /reference/format/format.md]

実装例

// 書式文字列のチェック
template <class CharT, class... Args>
consteval void fmt_checker(std::basic_string_view<CharT> str)
{
  //

  if (/*カッコの開き・閉じが一致しない場合*/) {
    throw "invalid brackets"; // throw式は定数式で実行できないため、
                              // このパスを通ったらコンパイルエラーになる
  }

  //

  if (/*型が合わない時*/) {
    throw "invalid type specifier";
  }

  //
}

namespace std {
  template <class CharT, class... Args>
  struct basic_format_string {
    std::basic_string_view<CharT> str;

    template <class T>
      requires std::convertible_to<const T&, std::basic_string_view<charT>>
    consteval basic_format_string(const T& s)
      : str(s)
    {
      fmt_checker<CharT, Args...>(str);
    }
  };

  template <class... Args>
  using format_string = basic_format_string<char, std::type_identity_t<Args>...>;
}
  • std::convertible_to[link /reference/concepts/convertible_to.md]
  • std::type_identity_t[link /reference/type_traits/type_identity.md]

バージョン

言語

  • C++23

処理系

参照