I'm a new hand of C++. When I read source code of node.js, I saw these code:
struct StartExecutionCallbackInfo {
v8::Local<v8::Object> process_object;
v8::Local<v8::Function> native_require;
};
using StartExecutionCallback =
std::function<v8::MaybeLocal<v8::Value>(const StartExecutionCallbackInfo&)>;
LoadEnvironment(env, StartExecutionCallback{});
My confusion is this one: StartExecutionCallback{}
.
I think it can be interpreted like this:
std::function<v8::MaybeLocal<v8::Value>(const StartExecutionCallbackInfo&)>{};
But what does it mean? A null function pointer?
As you probably know StartExecutionCallback
is a type for a function [wrapper] object, accepting const StartExecutionCallbackInfo&
as an argument, and returning a v8::MaybeLocal<v8::Value>
.
Using StartExecutionCallback{}
creates an instance of that type, but it's an instance that does not in fact contain any callable (i.e. the function wrapper has no target).
So yes, it is somewhat similar to a null function pointer.
LoadEnvironment
that accepts this argument, can check whether it is an actual callable or an an empty one. Attempting to call an "empty" std::function
will result in a std::bad_function_call
exception.
The code below demonstrates this (with a simplified version):
#include <iostream>
#include <functional>
//...
std::function<void(int)> f{};
if (f)
{
f(4);
}
else
{
std::cout << "f is empty\n";
}
The output will be:
f is empty