Check if std :: function "valid" function is correct in C ++ 11

I want to implement a dynamic task like this:

typedef std::function<void(void)> Job;
typedef std::function<Job(void)> JobGenerator;

// ..

JobGenerator gen = ...;
auto job = gen(); 
while (IsValidFunction(job))
{
    job();
}

How can i implement IsValidFunction? Is there a default value for std::functionto check for?

+4
source share
2 answers

You can simply check jobas bool:

while (auto job = gen())
{
    job();
}

This is a kind of abbreviation that assigns jobfrom gen()every time through the loop, stopping when jobevaluated as false, relying on std::function<>::operator bool: http://en.cppreference.com/w/cpp/utility/functional/function/operator_bool

+8
source

, , bool. , , . nullptr.

+1

All Articles