您的位置:首页 > 编程语言 > C语言/C++

C++11新特性应用--占位符(std::placeholders std::is_placeholder std::is_bind_expression)

2017-04-07 09:46 387 查看
上一篇博客讲了std::function和std::bind的使用,其中提到了占位符,std::placeholders

定义如下:
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: 5
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.
#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
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成员:
#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;
}
//输出
true
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: