c++ function

 code copied from clang/lib/Parse/ParseDecl.cpp#L4426

#include 
#include 
#include 
#include 
#include 
template 
struct remove_cvref // NOLINT(readability-identifier-naming)
{
  using type = std::remove_cv_t>;
};

template 
using remove_cvref_t // NOLINT(readability-identifier-naming)
    = typename remove_cvref::type;

template class function_ref;

template
class function_ref {
  Ret (*callback)(intptr_t callable, Params ...params) = nullptr;
  intptr_t callable;

  template
  static Ret callback_fn(intptr_t callable, Params ...params) {
    return (*reinterpret_cast(callable))(
        std::forward(params)...);
  }

public:
  function_ref() = default;
  function_ref(std::nullptr_t) {}

  template 
  function_ref(
      Callable &&callable,
      // This is not the copy-constructor.
      std::enable_if_t,
                                     function_ref>::value> * = nullptr,
      // Functor must be callable and return a suitable type.
      std::enable_if_t::value ||
                       std::is_convertible()(
                                               std::declval()...)),
                                           Ret>::value> * = nullptr)
      : callback(callback_fn::type>),
        callable(reinterpret_cast(&callable)) {}

  Ret operator()(Params ...params) const {
    return callback(callable, std::forward(params)...);
  }

  explicit operator bool() const { return callback; }
};
struct Data{
    int a;
    int b;
};
void test_callback(function_ref callback){
    Data data={1,3};
    callback(data);
}
void test_std_fun(std::function callback){
    Data data={12,3};
    callback(data);
}
int main(){
    int a=123;
    auto callback=[&](Data &data){
        std::cout<

Reference:
[1]c++ std::function

你可能感兴趣的:(c++ function)