C++11新特性应用--占位符(std::placeholders std::is_placeholder std::is_bind_expression)
2017-04-07 09:46
387 查看
上一篇博客讲了std::function和std::bind的使用,其中提到了占位符,std::placeholders
定义如下:
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
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.
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
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成员:
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
踩
定义如下:
namespace placeholders { extern /* unspecified */ _1; extern /* unspecified */ _2; extern /* unspecified */ _3; // ... }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
using namespace std::placeholders; auto bound_fn = std::bind (fn,100,_1); bound_fn(5); // calls fn(100,5), i.e.: replacing _1 by the first argument: 51
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.
#include <functional> #include <string> #include <iostream> void goodbye(const std::string& s) { std::cout << "Goodbye " << s << '\n'; } class Object { public: void hello(const std::string& s) { std::cout << "Hello " << s << '\n'; } }; int main(int argc, char* argv[]) { typedef std::function<void(const std::string&)> ExampleFunction; Object instance; std::string str("World"); ExampleFunction f = std::bind(&Object::hello, &instance, std::placeholders::_1); // equivalent to instance.hello(str) f(str); f = std::bind(&goodbye, std::placeholders::_1); // equivalent to goodbye(str) f(str); return 0; }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
#include <iostream> // std::cout, std::boolalpha #include <functional> // std::is_placeholder, std::placeholders int main () { using namespace std::placeholders; // introduces _1 std::cout << std::is_placeholder<decltype(_1)>::value << '\n'; std::cout << std::is_placeholder<decltype(_2)>::value << '\n'; std::cout << std::is_placeholder<int>::value << '\n'; return 0; } //输出: 1 2 01
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成员:
#include <iostream> // std::cout, std::boolalpha #include <functional> // std::bind, std::plus, std::placeholders, std::is_bind_expression int main () { using namespace std::placeholders; // introduces _1 auto increase_int = std::bind (std::plus<int>(),_1,1); std::cout << std::boolalpha; std::cout << std::is_bind_expression<decltype(increase_int)>::value << '\n'; return 0; } //输出 true1
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
踩
相关文章推荐
- C++11新特性应用--占位符(std::placeholders std::is_placeholder std::is_bind_expression)
- C++11新特性应用--占位符(std::placeholders std::is_placeholder std::is_bind_expression)
- C++11新特性应用--占位符(std::placeholders std::is_placeholder std::is_bind_expression)
- C++11新特性应用--实现延时求值(std::function和std::bind)
- C++11新特性应用--实现延时求值(std::function和std::bind)
- C++11新特性应用--实现延时求值(std::function和std::bind)
- c++11特性与cocos2d-x 3.0之std::bind与std::function
- c++11特性与cocos2d-x 3.0之std::bind与std::function
- c++11特性与cocos2d-x 3.0之std::bind与std::function
- c++11特性与cocos2d-x 3.0之std::bind与std::function
- 【读书笔记】【深入应用C++11】1.5 std::function和bind绑定器
- C++11新特性之std::bind与std::function
- c++11特性与cocos2d-x 3.0之std::bind与std::function
- C++11新特性:参数绑定——std::bind
- C++11新特性之std::function & std::bind
- 【c++11 新特性应用】利用bind实现通用的混合任务线程池
- c++11基础学习(2)std::bind与std::function特性
- C++11新特性之二——std::bind std::function 高级用法
- c++11 特性 std::function 与 std::bind
- 【C++11】新特性——std::function 和 std::bind