I have this example program:
#include <iostream>
template<typename Message, typename Decoration, typename PrintImpl>
void print_surrounded(Message&& msg, const Decoration& decoration, const PrintImpl& print_impl)
{
print_impl(decoration); // should forward be used?
print_impl(" ");
print_impl(std::forward<Message>(msg));
print_impl(" ");
print_impl(decoration);
}
template<typename Message, typename PrintImpl>
void pretty_print(Message&& msg, const PrintImpl& print_impl)
{
print_surrounded(std::forward<Message>(msg), "***", print_impl);
}
int main()
{
pretty_print("So pretty!", [](const char* msg) {
std::cout << msg;
});
}
I also posted it on Coliru.
As you can see I use different ways to pass the arguments:
&&
? If yes, should I also use std::forward
?)Am I making the right choices?
Am I making the right choices?
Yes (mostly).
Decoration is captured as const ref here because its value is used twice and I'm not sure if using forward twice would be safe. (It might be moved away by the first forward?)
Don't use std::forward
when you'd do it multiple times, exactly for the reason you layed out.
PrintImpl is captured as const reference because I don't see any reason to use forward.
What you might want to do is take PrintImpl&&
and don't use std::forward
(keeping them as lvalues), allowing function objects without const
-qualified operator()
to be passed.
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