Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I stumbled across this during my experiments with C++11. I find that it is an obvious solution, but I haven't been able to find any other examples of it in the wild, so I'm concerned that there's something I'm missing.

The practice I'm referring to (in the "addAsync" function):

#include <thread>
#include <future>
#include <iostream>
#include <chrono>

int addTwoNumbers(int a, int b) {
    std::cout << "Thread ID: " << std::this_thread::get_id() << std::endl;

    return a + b;
}

void printNum(std::future<int> future) {
    std::cout << future.get() << std::endl;
}

void addAsync(int a, int b, auto callback(std::future<int>) -> void) { //<- the notation in question
    auto res = std::async(std::launch::async, addTwoNumbers, a, b);

    if (callback) //super straightforward nullptr handling
        return callback(std::move(res));
}

int main(int argc, char** argv) {
    addAsync(10, 10, [](std::future<int> number) { //lambda functions work great
        addAsync(number.get(), 20, [](std::future<int> number) {
            addAsync(893, 4387, printNum); //as do standard functions
            addAsync(2342, 342, nullptr); //executes, sans callback

            std::cout << number.get() << std::endl;
        });
    });

    std::cout << "main thread: " << std::this_thread::get_id() << std::endl;

    return 0;
}

Is it considered bad practice, or is it non-portable (I've only tried it in MSVC++ 2015)? Also, how does the compiler treat this; by conversion to std::function?

I would love to keep using this in my projects, as it obviously states the required argument types and return type in the "signature", accepts a nullptr for optionality, and seems to "just work" (I am aware that these are famous last words in C++).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
292 views
Welcome To Ask or Share your Answers For Others

1 Answer

auto callback(std::future<int>) -> void is the declaration of a entity of type void(std::future<int>) called callback. When listed as an argument, the compiler adjusts this to be a pointer-to-function of type void(*)(std::future<int>).

Your lambda is stateless, and as such can be implicitly converted to a function pointer.

Once you add a non-trivial capture, your code will stop compiling:

[argc](std::future<int> number) { 
   std::cout << argc << '
';

...

Now, ignoring your question content and looking at the title...

There is a modest cost for a std::function because it is a value-type, not a view-type. As a value-type, it actually copies its argument.

You can get around this by wrapping the calling object in a std::ref, but if you want to state "I won't keep this function object around longer than this call", you can write a function_view type as follows:

template<class Sig>
struct function_view;

template<class R, class...Args>
struct function_view<R(Args...)> {
  void* ptr = nullptr;
  R(*pf)(void*, Args...) = nullptr;

  template<class F>
  using pF = decltype(std::addressof( std::declval<F&>() ));

  template<class F>
  void bind_to( F& f ) {
    ptr = (void*)std::addressof(f);
    pf = [](void* ptr, Args... args)->R{
      return (*(pF<F>)ptr)(std::forward<Args>(args)...);
    };
  }
  // when binding to a function pointer
  // even a not identical one, check for
  // null.  In addition, we can remove a
  // layer of indirection and store the function
  // pointer directly in the `void*`.
  template<class R_in, class...Args_in>
  void bind_to( R_in(*f)(Args_in...) ) {
    using F = decltype(f);
    if (!f) return bind_to(nullptr);
    ptr = (void*)f;
    pf = [](void* ptr, Args... args)->R{
      return (F(ptr))(std::forward<Args>(args)...);
    };
  }
  // binding to nothing:
  void bind_to( std::nullptr_t ) {
    ptr = nullptr;
    pf = nullptr;
  }       
  explicit operator bool()const{return pf;}

  function_view()=default;
  function_view(function_view const&)=default;
  function_view& operator=(function_view const&)=default;

  template<class F,
    std::enable_if_t< !std::is_same<function_view, std::decay_t<F>>{}, int > =0,
    std::enable_if_t< std::is_convertible< std::result_of_t< F&(Args...) >, R >{}, int> = 0
  >
  function_view( F&& f ) {
    bind_to(f); // not forward
  }

  function_view( std::nullptr_t ) {}

  R operator()(Args...args) const {
      return pf(ptr, std::forward<Args>(args)...);
  }
};

live example.

This is also useful in that it is a strictly simpler kind of type erasure than std::function, so it could be educational to go over it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...