I have functions that take values and pass them to other functions. Nobody down the chain will ever care if it's an rvalue reference and they just want to read the value. Do I need to use forwarding references or can I just use const&
like this?
template<class Arg>
void printOne(std::string& str, std::string_view fmt, const Arg& arg) {
if constexpr (std::is_same<char, Arg>::value) {
// ...
} else if constexpr (...) {
// ...
}
}
template<class ...Args>
std::string printFormatted(std::string_view fmt, const Args& ...args) {
std::string str;
(printOne(str, fmt, args), ...);
return str;
}
CodePudding user response:
No, you don't have to use forwarding references. const
references bind to temporary rvalues, so in general when move semantics or perfect forwarding is not required, a plain, garden-variety const
reference will work.