上一篇博客讲了std::function和std::bind的使用,其中提到了占位符,std::placeholders
定义如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
This namespace declares an unspecified number of objects: _1, _2, _3,…, which are used to specify placeholders in calls to function bind.
When the function object returned by bind is called, an argument with placeholder _1 is replaced by the first argument in the call, _2 is replaced by the second argument in the
- 1
- 2
- 3
- 1
- 2
- 3
The type of these placeholder objects is unspecified (it depends on the library implementation, see is_placeholder), but in all cases their type shall at least be nothrow default-constructible and nothrow copy-constructible. Whether assignment operations or additional constructors are supported is implementation-defined, but any copy-assignment or move-constructor shall also be not throwing.
When a call to bind is used as a subexpression in another call to bind, the placeholders are relative to the outermost bind expression.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
C++11还有一个std::is_placeholder:
作用为:
identifies whether T is a bind placeholder.
就是判断T是否为占位符!!
std::is_placeholder具有一个成员变量:value
If T is the type of a placeholder:
T is the placeholder’s order number (1 for _1, 2 for _2, …).
else
T is 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
接下来就再解释一个:
std::is_bind_expression
用来判断是否为bind表达式:
同样具有value成员:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
唯一需要注意的就是,这个返回的是true或是false,与is_placeholder有点区别~
-
顶
- 1
-
踩