Here is a simple example;
template <typename T>
void foo(T t) {}
std::string str("some huge text");
foo(str);
My question is how can I force the compiler to pass str by reference without modifying function foo?
Pass the reference type explicitly:
template <typename T>
void foo(T t) {}
int main() {
std::string str("some huge text");
foo<std::string&>(str);
}
This does modify the function instantiation that you get (by generating a void foo<std::string&>(std::string& t)
), but it doesn't modify the function template.
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