|
| 1 | +# 推論補助 |
| 2 | +* future[meta header] |
| 3 | +* std[meta namespace] |
| 4 | +* packaged_task[meta class] |
| 5 | +* cpp20[meta cpp] |
| 6 | + |
| 7 | +```cpp |
| 8 | +namespace std { |
| 9 | + template <class R, class... ArgTypes> |
| 10 | + packaged_task(R(*)(ArgTypes...)) |
| 11 | + -> packaged_task<R(ArgTypes...)>; // (1) C++20 |
| 12 | + |
| 13 | + template <class F> |
| 14 | + packaged_task(F) |
| 15 | + -> packaged_task<Signature>; // (2) C++20 |
| 16 | +} |
| 17 | +``` |
| 18 | +
|
| 19 | +## 概要 |
| 20 | +`std::packaged_task`クラステンプレートの型推論補助。 |
| 21 | +
|
| 22 | +- (1) : 関数ポインタからの推論 |
| 23 | +- (2) : 関数オブジェクトからシグニチャの推論。このオーバーロードは、関数呼び出し演算子がひとつだけオーバーロードされている場合に有効 |
| 24 | +
|
| 25 | +
|
| 26 | +## テンプレートパラメータ制約 |
| 27 | +- (2) : |
| 28 | + - `&F::operator()`は評価されないオペランドとして扱われ、以下のいずれかの場合に適格である: |
| 29 | + - C++17 : |
| 30 | + - `decltype(&F::operator())`は、型`G`があるとして、`R(G::*)(A...) cv &(opt) noexcept(opt)`形式であること |
| 31 | + - C++26 : |
| 32 | + - `F::operator()`が非静的メンバ関数であり、`decltype(&F::operator())`は、型`G`があるとして、`R(G::*)(A...) cv &(opt) noexcept(opt)`形式もしくは`R(*)(G cv ref(opt), A...) noexcept(opt)`形式であること |
| 33 | + - `F::operator()`静的メンバ関数であり、`decltype(&F::operator())`は`R(*)(A...) noexcept(opt)`形式であること |
| 34 | +
|
| 35 | +
|
| 36 | +## 例 |
| 37 | +```cpp example |
| 38 | +#include <iostream> |
| 39 | +#include <future> |
| 40 | +
|
| 41 | +int foo(int, char) { return 0; } |
| 42 | +
|
| 43 | +struct Functor { |
| 44 | + int operator()(double) { return 1; } |
| 45 | +}; |
| 46 | +
|
| 47 | +int main() |
| 48 | +{ |
| 49 | + // (1) |
| 50 | + // 関数ポインタからの型推論 |
| 51 | + std::packaged_task f{foo}; |
| 52 | + f(1, '3'); |
| 53 | + std::cout << f.get_future().get() << std::endl; |
| 54 | +
|
| 55 | + // (2) |
| 56 | + // 関数オブジェクトからの型推論。 |
| 57 | + // 関数呼び出し演算子がひとつだけオーバーロードされていること |
| 58 | + std::packaged_task g{Functor{}}; |
| 59 | + g(1.23); |
| 60 | + std::cout << g.get_future().get() << std::endl; |
| 61 | +
|
| 62 | + // (3) |
| 63 | + // ラムダ式からの型推論 |
| 64 | + std::packaged_task h{[](int) { return 2; }}; |
| 65 | + h(3); |
| 66 | + std::cout << h.get_future().get() << std::endl; |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +### 出力 |
| 71 | +``` |
| 72 | +0 |
| 73 | +1 |
| 74 | +2 |
| 75 | +``` |
| 76 | + |
| 77 | + |
| 78 | +## バージョン |
| 79 | +### 言語 |
| 80 | +- C++20 |
| 81 | + |
| 82 | +### 処理系 |
| 83 | +- [Clang](/implementation.md#clang): 18 |
| 84 | +- [GCC](/implementation.md#gcc): 11 |
| 85 | +- [Visual C++](/implementation.md#visual_cpp): ?? |
| 86 | + |
| 87 | + |
| 88 | +## 関連項目 |
| 89 | +- [C++17 クラステンプレートのテンプレート引数推論](/lang/cpp17/type_deduction_for_class_templates.md) |
| 90 | +- [C++23 `this`ポインタをもつ必要のない演算子を`static`として宣言できるようにする](/lang/cpp23/static_operator.md) |
| 91 | +- [C++23 自身のオブジェクトを明示的にパラメータとして指定する](/lang/cpp23/deducing_this.md.nolink) |
| 92 | + |
| 93 | + |
| 94 | +## 参照 |
| 95 | +- [P0433R2 Toward a resolution of US7 and US14: Integrating template deduction for class templates into the standard library](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0433r2.html) |
| 96 | + |
0 commit comments