Comparing concatenated strings using a proxy

Take a look at this code snippet, which concatenates two strings and compares the result:

auto func_a() { 
  auto a = std::string{"Cole"}; 
  auto b = std::string{"Porter"}; 
  auto c = std::string{"ColePorter"}; 
  auto is_cole_porter = (a + b) == c; 
  // is_cole_porter is true 
}

Here is a visual representation of the preceding code snippet:

Accumulating two strings into a new string

The problem, here, is that (a + b) constructs a new temporary string in order to compare it with c. Instead of constructing a new string, we could just compare the concatenation right away, like this:

auto is_concat_equal( const ...

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.