名前空間
変種
操作

Lambda functions (C++11およびそれ以降)

提供: cppreference.com
< cpp‎ | language


 
 
C + +言語
一般的なトピック
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
フロー制御
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
条件付き実行文
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
繰り返し文
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
文をジャンプします
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
関数の宣言
ラムダ関数の宣言
関数テンプレート
の歴史。インライン指定
例外仕様 (廃止予定)
noexcept指定子 (C++11)
例外
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
名前空間
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
指定子
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CV指定
貯蔵期間指定
constexprの指定子 (C++11)
自動指定 (C++11)
alignas指定子 (C++11)
初期化
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
リテラル
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
代替表現
ユーティリティ
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
タイプ
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
型の別名宣言 (C++11)
属性 (C++11)
キャストします
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
暗黙の型変換
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
Cスタイルキャストと機能
メモリの割り当て
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラス固有の機能特性
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
明示 (C++11)
静的
特殊なメンバ関数
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
テンプレート
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
クラステンプレート
関数テンプレート
テンプレートの特殊化
パラメーターパック (C++11)
その他
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
インラインアセンブリ
 
閉鎖構築:スコープ内の変数を捕捉することができる無名の関数オブジェクトを.
Original:
Constructs a 閉鎖: an unnamed function object capable of capturing variables in scope.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集] 構文

[ capture ] ( params ) mutable exception attribute -> ret { body } (1)
[ capture ] ( params ) -> ret { body } (2)
[ capture ] ( params ) { body } (3)
[ capture ] { body } (4)
1)
完全な宣言
Original:
Full declaration
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
constのlambdaの宣言:コピーによって取得したオブジェクトを変更することはできません.
Original:
Declaration of a const lambda: the objects captured by copy cannot be modified.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
末尾のリターン型省略:クロージャのoperator()の戻り値の型は、次の規則に従って導き出される
Original:
Omitted trailing-return-type: the return type of the closure's operator() is deduced according to the following rules:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • bodyが単一return文で構成されている場合は、戻り値の型が返される式(右辺値から左辺値、配列からポインタ、または関数からポインタへの暗黙の型変換後)の一種です
    Original:
    if the body consists of the single return statement, the return type is the type of the returned expression (after rvalue-to-lvalue, array-to-pointer, or function-to-pointer implicit conversion)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • そうでなければ、戻り値の型はvoidです
    Original:
    otherwise, the return type is void
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
4)
省略されたパラメータリスト:パラメータリストは()だったかのように、関数は、引数を取りません
Original:
Omitted parameter list: function takes no arguments, as if the parameter list was ()
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集] 説明

mutable -
bodyがコピーによってキャプチャパラメータを変更するには、それらの非constメンバ関数を呼び出すことができます
Original:
allows body to modify the parameters captured by copy, and to call their non-const member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
exception -
閉鎖型の例外仕様またはオペレータ用句をnoexcept()を提供します
Original:
provides the 例外仕様 or the 句をnoexcept for operator() of the closure type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
attribute -
閉鎖型の演算子()の属性指定を提供しています
Original:
provides the 属性指定 for operator() of the closure type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

{{par | capture |{{tr| 関数が宣言されたスコープで見えるシンボルが関数本体内部が見えるかを指定します。 シンボルのリストは次のように渡すことができます。

  • [a,&b]どこ A値によって捕捉され、 bは参照によってキャプチャされます。
  • [this][[CPP /言語/これをキャプチャします| specifies which symbols visible in the scope where the function is declared will be visible inside the function body.

A list of symbols can be passed as follows:

  • [a,&b] where a is captured by value and b is captured by reference.
  • [this] captures the [[cpp/language/this}}|this pointer]]
  • [&] captures all symbols by reference
  • [=] captures all by value
  • [] captures nothing }}
params -
関数の名前のようにパラメータのリスト
Original:
The list of parameters, as in 関数の名前
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ret -
型を返します。存在しない場合には、関数のreturn文(またはそれがどのような値を返さない場合には無効)によって暗示だ
Original:
Return type. If not present it's implied by the function return statements ( or void if it doesn't return any value)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
body -
関数本体
Original:
Function body
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


ラムダ式は、次のメンバがあります閉鎖型として知られているユニークな名前のない非組合の非集計タイプの無名の一時オブジェクトを構築します
Original:
The lambda expression constructs an unnamed temporary object of unique unnamed non-union non-aggregate type, known as closure type, which has the following members:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

ClosureType ::
Original:
ClosureType::
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
operator()

ret operator()(params) const { body }
(キーワードは可変使用されませんでした)
ret operator()(params) { body }
(キーワードは可変使用されていました)

Executes the body of the lambda-expression, when invoked. When accessing a variable, accesses its captured copy (for the entities captured by copy), or the original object (for the entities captured by reference). Unless the keyword mutable was used in the lamda-expression, the objects that were captured by copy are non-modifiable from inside this operator().

Dangling references

If an entity is captured by reference, implicitly or explicitly, and the function call operator of the closure object is invoked after the entity's lifetime has ended, undefined behavior occurs. The C++ closures do not extend the lifetimes of the captured references.

ClosureType ::
Original:
ClosureType::
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
operator ret(*)(params)

typedef ret(*F)(params);
operator F() const;

This member function is only defined if the capture list of the lambda-expression is empty.

The value returned by this conversion function is a function pointer that, when invoked, has the same effect as invoking the closure object's function call operator directly.

ClosureType ::
Original:
ClosureType::
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ClosureType()

ClosureType() = delete;
ClosureType(const ClosureType& ) = default;
ClosureType(ClosureType&& ) = default;

Closure types are not DefaultConstructible. The copy constructor and the move constructor are implicitly-declared and may be implicitly-defined according to the usual rules for implicit コピーコンストラクタ and コンストラクタに移動します.

ClosureType ::
Original:
ClosureType::
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
operator=()

ClosureType& operator=(const ClosureType&) = delete;

Closure types are not CopyAssignable.

ClosureType ::
Original:
ClosureType::
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
~ClosureType()

~ClosureType() = default;

The destructor is implicitly-declared.

ClosureType ::
Original:
ClosureType::
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
CapturedParam

T1 a;

T2 b;

...

If the lambda-expression captures anything by copy (either implicitly with capture clause [=] or explicitly with a capture that does not include the character &, e.g. [a, b, c]), the closure type includes unnamed non-static data members, declared in unspecified order, that hold copies of all entities that were so captured.

The type of each data member is the type of the corresponding captured entity, except if the entity has reference type (in that case, references to functions are captured as-is, and references to objects are captured as copies of the referenced objects).

For the entities that are captured by reference (with the default capture [&] or when using the character &, e.g. [&a, &b, &c]), it is unspecified if additional data members are declared in the closure type.

[編集]

この例では、ラムダ宣言に起因する一般的アルゴリズムとそのオブジェクトにラムダを渡す方法を示し、std::functionオブジェクトに格納することができます.
Original:
This example shows how to pass a lambda to a generic algorithm and that objects resulting from a lambda declaration, can be stored in std::function objects.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
 
int main()
{
    std::vector<int> c { 1,2,3,4,5,6,7 };
    int x = 5;
    c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end());
 
    std::cout << "c: ";
    for (auto i: c) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
 
    std::function<int (int)> func = [](int i) { return i+4; };
    std::cout << "func: " << func(6) << '\n'; 
}

Output:

c: 5 6 7
func: 10

[編集] も参照してください

自動指定
表現(C++11)によって定義された型を指定します
Original:
specifies a type defined by an expression (C++11)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[edit]
(C++11)
指定された関数呼び出しシグネチャを持つ任意の型の呼び出し可能オブジェクトをラップします
Original:
wraps callable object of any type with specified function call signature
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(クラステンプレート) [edit]