Skip to content

Latest commit

 

History

History
75 lines (56 loc) · 1.9 KB

File metadata and controls

75 lines (56 loc) · 1.9 KB

constant_arg_t

  • utility[meta header]
  • std[meta namespace]
  • class template[meta id-type]
  • cpp26[meta cpp]
namespace std {
  template <auto V>
  struct constant_arg_t {
    explicit constant_arg_t() = default;
  };

  template <auto V> constexpr constant_arg_t<V> constant_arg{};
}

概要

constant_arg_tクラスは、オーバーロードのための空クラスである。

標準ライブラリの特定機能において、定数引数をテンプレートパラメータとして受け取って構築するための関数オーバーロードを定義するためにある。

備考

デフォルトコンストラクタにexplicitが付いているのは、constant_arg_t<F> x = {};のように=付きの波カッコ初期化を禁止するためである。ユーザーは通常、constant_arg_t型の変数テンプレートとして事前定義されているconstant_argを使用すればよいので、問題にはならない。

#include <functional>
#include <print>
#include <utility>

void call(std::function_ref<void()> fn)
{
  fn();
}

struct Dog {
  void cry() { std::println("Bow-wow"); }
};

int main()
{
  Dog dog;
  call({std::constant_arg<&Dog::cry>, dog});
}
  • std::constant_arg[color ff0000]
  • std::function_ref[link /reference/functional/function_ref.md]

出力

Bow-wow

バージョン

言語

  • C++26

処理系

関連項目

参照