C++11新特性应用--占位符(std::placeholders std::is_placeholder std::is_bind_expression)
来源:互联网 发布:什么是云计算举例说明 编辑:程序博客网 时间:2024/06/01 22:22
上一篇博客讲了std::function和std::bind的使用,其中提到了占位符,std::placeholders
定义如下:
namespace placeholders { extern /* unspecified */ _1; extern /* unspecified */ _2; extern /* unspecified */ _3; // ...}
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
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;}
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::placeholdersint 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;}//输出:120
接下来就再解释一个:
std::is_bind_expression
用来判断是否为bind表达式:
同样具有value成员:
#include <iostream> // std::cout, std::boolalpha#include <functional> // std::bind, std::plus, std::placeholders, std::is_bind_expressionint 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
唯一需要注意的就是,这个返回的是true或是false,与is_placeholder有点区别~
- 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::placeholders
- C++11新特性之std::bind与std::function
- C++11新特性之 std::future and std::async
- C++11新特性之std::function & std::bind
- C++11新特性之 std::future and std::async
- C++11新特性应用--实现延时求值(std::function和std::bind)
- 【C/C++】C++11新特性:std::bind
- C++11新特性之std::function
- C++11新特性之std::function
- std
- std
- std
- C++11 std::bind std::function 高级用法 (c++11的新特性)
- 【C++11】新特性——std::function 和 std::bind
- 【C++11】新特性——std::function
- Android.mk 详解
- MySQL 进阶
- “顶层const”和“底层const”的思考
- android之旅24 多媒体编程:音乐、视频、拍照、视频
- 用户界面开发基础
- C++11新特性应用--占位符(std::placeholders std::is_placeholder std::is_bind_expression)
- UC/OS-II基础知识之任务的同步与事件
- phpcms 站点管理增加联系方式备案等字段等
- 非常好的理解遗传算法的例子 .
- hdu 1757 A Simple Math Problem 矩阵
- (实训第二天)用C程序判断机器是大端模式还是小端模式,附源程序
- Java调用Oracle存储过程
- COCOS学习笔记--Cocod2dx内存管理(三)-Coco2d-x内存运行原理
- 常用互动方法