I've got a function like this:
void loadData(std::function<void (std::string, std::string, std::string)> callback)
{
// data loading stuff
callback(body, subject, header);
}
The problem is I'm not necessarily need to use subject
and header
in my callback function. Now I'm handling it this way:
loadData([](std::string body, std::string, std::string){
std::cout << body;
})
I want to replace it with
loadData([](std::string body){
std::cout << body;
})
and automatically pass to callback function as many arguments as it able to accept.
I don't want to manually overload loadData
function for all 3 possible argument counts. I also don't want to use any more complicated lambda syntax on the calling site because my library should be clear for others to use.
Is this possible using C++ STL and Boost?
What about ignoring the following arguments using ...
?
loadData([](std::string body, ...){
std::cout << body;
})
To avoid this problem, if you can use C++14, you can use auto ...
(better auto && ...
to avoid unnecessary copies; thanks Yakk).
loadData([](std::string body, auto && ...){
std::cout << body;
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With