How to properly check if std::function is empty in C++11?

隐身守侯 提交于 2019-12-03 10:21:03

问题


I was wondering how to properly check if an std::function is empty. Consider this example:

class Test {
    std::function<void(int a)> eventFunc;

    void registerEvent(std::function<void(int a)> e) {
        eventFunc = e;
    }

    void doSomething() {
        ...
        eventFunc(42);
    }
};

This code compiles just fine in MSVC but if I call doSomething() without initializing the eventFunc the code obviously crashes. That's expected but I was wondering what is the value of the eventFunc? The debugger says 'empty'. So I fixed that using simple if statement:

   void doSomething() {
        ...
        if (eventFunc) {
            eventFunc(42);
        }
   }

This works but I am still wondering what is the value of non-initialized std::function? I would like to write if (eventFunc != nullptr) but std::function is (obviously) not a pointer.

Why the pure if works? What's the magic behind it? And, is it the correct way how to check it?


回答1:


You're not checking for an empty lambda, but whether the std::function has a callable target stored in it. The check is well-defined and works because of std::function::operator bool which allows for implicit conversion to bool in contexts where boolean values are required (such as the conditional expression in an if statement).

Besides, the notion of an empty lambda doesn't really make sense. Behind the scenes the compiler converts a lambda expression into a struct (or class) definition, with the variables you capture stored as data members of this struct. A public function call operator is also defined, which is what allows you to invoke the lambda. So what would an empty lambda be?


You can also write if(eventFunc != nullptr) if you wish to, it's equivalent to the code you have in the question. std::function defines operator== and operator!= overloads for comparing with a nullptr_t.




回答2:


Check here http://www.cplusplus.com/reference/functional/function/operator_bool/

Example

// function::operator bool example
#include <iostream>     // std::cout
#include <functional>   // std::function, std::plus

int main () {
  std::function<int(int,int)> foo,bar;
  foo = std::plus<int>();

  foo.swap(bar);

  std::cout << "foo is " << (foo ? "callable" : "not callable") << ".\n";
  std::cout << "bar is " << (bar ? "callable" : "not callable") << ".\n";

  return 0;
}

Output

foo is not callable.

bar is callable.




回答3:


(Let me provide a clear answer.)

You can check if a std::function is empty with std::function::operator bool.

true: if the object is callable.
false: otherwise (the object is an empty function)

Example

#include <iostream>
#include <functional>

int main ()
{
    std::function<int(int,int)> foo = std::plus<int>();//assigned: not empty
    std::function<int(int,int)> bar;//not assigned: empty

    std::cout << "foo is " << (foo ? "not empty" : "empty") << ".\n";
    std::cout << "bar is " << (bar ? "not empty" : "empty") << ".\n";

    return 0;
}

Output

foo is not empty.
bar is empty.



来源:https://stackoverflow.com/questions/21806632/how-to-properly-check-if-stdfunction-is-empty-in-c11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!