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

上一篇博客讲了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 
#include 
#include 

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      // std::cout, std::boolalpha
#include    // 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

接下来就再解释一个:
std::is_bind_expression
用来判断是否为bind表达式:
同样具有value成员:

#include      // std::cout, std::boolalpha
#include    // 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

唯一需要注意的就是,这个返回的是true或是false,与is_placeholder有点区别~

你可能感兴趣的:(C++,浅析C++11新特性)