One solution to the problem is to create a derived interface with an additional method that returns the return value as temporary
template <class T> class TypedReturnValueHolder : public ReturnValueHolder { public: virtual T rv() = 0; };
and they modify the original ReturnValueHolder
template <class T> class ReturnValueWrapper : public ReturnValueHolder { public: typename no_cref<T>::type rv; ReturnValueWrapper(T rv) : rv(rv) {} };
to inherit and implement a derived interface.
template <class T> class ReturnValueWrapper : public TypedReturnValueHolder<T> { typename no_cref<T>::type prv; public: ReturnValueWrapper(T rv) : prv(rv) {} virtual T rv() { return prv; }; };
Once this is done, the return from DoExpectation can be written as
if (call->retVal) return ((TypedReturnValueHolder<Z> *)call->retVal)->rv();
Example from rewriting question for using Do
mocks.OnCall( foo, IFoo::foo ) .Do( [](){ return std::unique_ptr<IFoo>(); } );
then compiled and executed as expected.
source share