Code: Select all
EXPECT_CALL(my_mock, foo(_))
.WillRepeatedly(InvokeWithoutArgs([](){
static int counter = 0;
constexpr static std::array values {13, 26};
return values[counter++ % values.size()];
}) );
< /code>
Meine erste Intuition bestand darin, die Antwort stattdessen mit einem veränderlichen Zähler zu kommentieren oder zu bearbeiten: < /p>
[counter = 0]() mutable { ... }
Code: Select all
#include
auto static_lambda() {
return []() { static int i = 0; return i++; }
}
auto mutable_lambda() {
return [i = 0]() mutable { return i++; }
}
int main() {
auto s = static_lambda();
std::printf("%d,", s());
std::printf("%d,", s());
auto s2 = static_lambda();
std::printf("%d,", s2());
std::printf("%d,", s2());
auto m = mutable_lambda();
std::printf("%d,", m());
std::printf("%d,", m());
auto m2 = mutable_lambda();
std::printf("%d,", m2());
std::printf("%d,", m2());
}