Gmock returning a mock by value

Skip to first unread message

Max Davies-Smith

Feb 22, 2016, 10:34:05 AM2/22/16


I think the answer is 'no dont be silly' but it's worth asking.
I have a function the returns objects by value. Internally it uses std::move on the return but probably an irrelevant point since elision is there to help.
What this function returns is a type I have mocked. On which I have set expectations.
Firstly, it don't compile
Secondly if it did the expectations wouldn't work because it would be a copy - i guess.
It's annoying to have to return a smart ptr when I really don't want the heap cost and the scope is small.
So is there a gmock way to do what is typical and optimal ?

Corey Kosak

Feb 24, 2016, 7:18:42 PM2/24/16
to Max Davies-Smith, Google C++ Mocking Framework
I don't see how this could ever work.

The mock class is a subclass of the original class. If your return type is that of the original class, you're going to slice the object, just as you would in any other C++ inheritance scenario (i.e. this is a problem not limited to gMock).

It might--might--be possible to do this by using the techniques described here under "Mocking Nonvirtual Methods", but one thing I'm not sure about is whether--as you say--the expectations are copied when the object is copied.


You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
For more options, visit

Reply all
Reply to author
0 new messages