M
Matt Fioravante
I have this function defined:
bool walk_directory_tree(const char* dirname,
std::function<bool(const char*, const char*)> cb,
bool recurse=false,
const char* match_suffix=nullptr,
bool returndots=false);
In this particular case, the std::function object never outlives its caller.
Therefore we would like to pass it a reference instead of a full function
object to save on copies and possible memory allocation.
An example call would be:
auto cb = [&](const char* full, const char* base) { //lots of stuff };
walk_directory_tree("some/directory/path", std::cref(cb));
With std::cref, the lambda is passed into the function object by reference
instead of by value. Since the lambda exists for the lifetime of the
walk_directory_tree() call, this is always safe to do.
My question is there any way to make it so that the std::function object always
takes its argument by reference without requiring the caller to remember to do
std::ref or std::cref? I don't see any possible use case here for pass by
value.
Or am I not understanding something about std::function?
bool walk_directory_tree(const char* dirname,
std::function<bool(const char*, const char*)> cb,
bool recurse=false,
const char* match_suffix=nullptr,
bool returndots=false);
In this particular case, the std::function object never outlives its caller.
Therefore we would like to pass it a reference instead of a full function
object to save on copies and possible memory allocation.
An example call would be:
auto cb = [&](const char* full, const char* base) { //lots of stuff };
walk_directory_tree("some/directory/path", std::cref(cb));
With std::cref, the lambda is passed into the function object by reference
instead of by value. Since the lambda exists for the lifetime of the
walk_directory_tree() call, this is always safe to do.
My question is there any way to make it so that the std::function object always
takes its argument by reference without requiring the caller to remember to do
std::ref or std::cref? I don't see any possible use case here for pass by
value.
Or am I not understanding something about std::function?