• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    ラムダ式に対する属性 [P2173R1]

    このページはC++23に採用された言語機能の変更を解説しています。

    のちのC++規格でさらに変更される場合があるため関連項目を参照してください。

    概要

    C++23では、ラムダ式のいくつかの箇所に属性を指定できる。

    • ラムダ式の関数呼び出し演算子自体に対する属性
      • キャプチャ指定のうしろ
        • テンプレートのラムダ式の場合は、テンプレートパラメータリストに続くrequires定義のうしろ
      • C++23時点でここに指定できる属性は以下
    • ラムダ式の修飾もしくは戻り値型に対する属性
      • パラメータリスト、constexpr / mutablenoexceptのうしろ (このあとに後置戻り値型、requiresが続く)
      • C++23時点でここに指定できる標準属性なし

    #include <stdexcept>
    #include <concepts>
    
    int main()
    {
      auto f1 = [] [[noreturn]] { throw std::runtime_error("error"); };
      auto f2 = [] [[nodiscard]] { return true; };
      auto f3 = [] [[deprecated]] { return false; };
    
      auto f4 = [] <class T>
        requires std::copy_constructible<T>
        [[nodiscard]]
        (T value) {
          return value;
      };
    }
    

    出力

    関連項目

    参照