Assigning a concatenated proxy

Now you might be thinking, what if we actually want to store the concatenated string as a new string rather than just compare it? What we do is simply overload an operator String() method so that the concatenation of the strings can implicitly convert itself to a string, like this:

struct ConcatProxy { 
  const std::string& a; 
  const std::string& b; 
  operator String() const && { return String{a + b}; } 
}; 
 
auto func() { 
  String c = String{"Marc"} + String{"Chagall"}; 
} 

There is one little snag though; we cannot initialize the new String object with the auto keyword, as this would result in ConcatProxy:

auto c = String{"Marc"} + String{"Chagall"}; // c is a ConcatProxy due to the auto 

Unfortunately, we have no way ...

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.