KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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: Message is passed as a universal reference because it eventually needs to be forwarded to the PrintImpl function. Decoration is passed 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?) PrintImpl is passed as const reference because I don't see any reason to use forward. However, I'm not certain if this is wise. (Should I pass by && ? If yes, should I also use std::forward ?) Am I making the right choices?
Tags (comma-separated)
Save Edits
Cancel