Forwarding links and converting constructors in C ++

Suppose I want to create a function that takes both the arguments of the string lvalue and rvalue by reference, converts them to uppercase and outputs them to standard output:

void upper_print(std::string& s);
void upper_print(std::string&& s);

This works fine:

std::string s("Hello world");
upper_print(s);
upper_print(std::string("Hello world"));
upper_print("Hello world"); // converting ctor used

However, to avoid redundancy, I want to use the forwarding link instead:

template <typename T> upper_print(T&& s);

Unfortunately, I cannot call upper_printwith a string literal argument:

std::string s("Hello world"); // OK
upper_print(s); // OK
upper_print(std::string("Hello world")); // OK
upper_print("Hello world"); // ERROR

I am aware of the possibility of restricting arguments to objects std::string, for example with std::enable_ifor static_assert. But here it does not help.

Is it possible to combine link forwarding and constructor conversion functions in this sense?

+6
1

, ?

template <typename T>
void upper_print_helper(T&& s);

inline void upper_print(std::string&& s) {
    upper_print_helper(std::move(s));
}
inline void upper_print(std::string& s) {
    upper_print_helper(s);
}
0

All Articles