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

履歴 編集

class template
<utility>

std::nontype_t(C++26)

namespace std {
  template <auto V>
  struct nontype_t {
    explicit nontype_t() = default;
  };

  template <auto V> constexpr nontype_t<V> nontype{};
}

概要

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

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

備考

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

#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::nontype<&Dog::cry>, dog});
}

出力

Bow-wow

バージョン

言語

  • C++26

処理系

関連項目

参照