You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
static_assert(sizeof(U) != sizeof(U), "Return() cannot take an rvalue references for functions returning a reference because it would make it dangling, use ReturnCapture() instead.");
78
+
static_assert(sizeof(U) != sizeof(U), "Return() cannot take an rvalue references for functions returning a reference because it would make it dangling, use ReturnValCapt() instead.");
77
79
returnReturn(r); // Only written to silence warning about not returning from a non-void function, but will never be executed.
78
80
}
79
81
80
82
voidAlwaysReturn(const R &r) {
81
-
returnAlwaysDo([&r](consttypename fakeit::test_arg<arglist>::type...) -> R { return r; });
83
+
AlwaysDo([&r](consttypename fakeit::test_arg<arglist>::type...) -> R { return r; });
82
84
}
83
85
84
-
template <typename U = R>
86
+
// The std::enable_if is only there to disambiguate with the deprecated version of .AlwaysReturn<type>(val), and
87
+
// can be removed once that deprecated version is removed.
88
+
template <typename U = R, typename std::enable_if<std::is_reference<U>::value, bool>::type = true>
85
89
voidAlwaysReturn(fk_remove_cvref_t<R>&&) {
86
-
static_assert(sizeof(U) != sizeof(U), "AlwaysReturn() cannot take an rvalue references for functions returning a reference because it would make it dangling, use AlwaysReturnCapture() instead.");
90
+
static_assert(sizeof(U) != sizeof(U), "AlwaysReturn() cannot take an rvalue references for functions returning a reference because it would make it dangling, use AlwaysReturnValCapt() instead.");
"The type captured by ReturnCapture() (named T) must be compatible with the return type of the function (named R), i.e. T t{...}; R& r = t; must compile without creating temporaries.");
93
-
auto store = std::make_shared<fk_remove_cvref_t<T>>(std::forward<T>(r));
"The type captured by AlwaysReturnCapture() (named T) must be compatible with the return type of the function (named R), i.e. T t{...}; R& r = t; must compile without creating temporaries.");
103
-
auto store = std::make_shared<fk_remove_cvref_t<T>>(std::forward<T>(r));
104
-
returnAlwaysDo([store](consttypename fakeit::test_arg<arglist>::type...) mutable -> R {
107
+
template<typename T = R>
108
+
voidAlwaysReturnValCapt(T&& r) {
109
+
// If a ref to T can be cast to a ref to R, then store T.
110
+
// Otherwise, create an object R constructed from the received T and store it.
static_assert(std::is_lvalue_reference<T>::value, "ReturnRefCapt() cannot take an rvalue references because it would make it dangling, use ReturnValCapt() instead.");
168
+
returnDo([&r](consttypename fakeit::test_arg<arglist>::type...) -> R { return r; });
169
+
}
170
+
171
+
template<typename T>
172
+
voidAlwaysReturnRefCapt(T&& r) {
173
+
static_assert(std::is_lvalue_reference<T>::value, "AlwaysReturnRefCapt() cannot take an rvalue references because it would make it dangling, use AlwaysReturnValCapt() instead.");
174
+
AlwaysDo([&r](consttypename fakeit::test_arg<arglist>::type...) -> R { return r; });
129
175
}
130
176
};
131
177
@@ -146,6 +192,38 @@ namespace fakeit {
146
192
using helper::BasicReturnImplHelper<R, arglist...>::Return;
147
193
using helper::BasicReturnImplHelper<R, arglist...>::AlwaysReturn;
148
194
195
+
// DEPRECATED: This should ideally be removed, it allows writing .Return<std::string>("ok") when a function
196
+
// returns "const std::string&" (for example) to have the same behavior has .ReturnValCapt("ok"). But it is prone
197
+
// to errors (because you have to specify the type). .ReturnValCapt("ok") is superior and should be used instead.
0 commit comments