CJCoding With Joseph
15free / daySign inCreate account

Print Foo Differently for Lvalues and Rvalues

Most of the class template Foo<T> is written for you: it stores a value and keeps a static printCount per specialization that starts at 10 and can only be changed through addToPrintCount. Write two operator<< overloads as free function templates, both returning std::ostream&:

- operator<<(std::ostream& os, Foo<T>& foo): adds 1 to the counter, then prints Lvalue Foo: , the value, count= and the updated counter - operator<<(std::ostream& os, Foo<T>&& foo): adds 2 to the counter, then prints Rvalue Foo: , the value, count= and the updated counter

Each line ends with a newline and each overload returns os. Because printCount is static, Foo<int> and Foo<std::string> count separately. The tests stream into an std::ostringstream and print everything at the end under a Captured: heading, so write to the os parameter, never to std::cout directly. They stream a named Foo<int> holding 5, a temporary Foo<int>(7), then std::move of the named int object, then a named Foo<std::string> holding hi and std::move of it, five lines in that order.

Do NOT write a main function.

Expected Output:

Captured:
Lvalue Foo: 5 count=11
Rvalue Foo: 7 count=13
Rvalue Foo: 5 count=15
Lvalue Foo: hi count=11
Rvalue Foo: hi count=13

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Templates
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.