Closed oriolarcas closed 3 years ago
If a method returns by constant reference, a Do/AlwaysDo lambda that returns by value will compile without warnings or errors:
struct Test { virtual const int& test_method() = 0; virtual ~Test() = default; }; TEST_CASE("test case", "[test]") { Mock<Test> test_mock; int var = 42; When(Method(test_mock, test_method)).AlwaysDo([&var]() -> int { return var; }); REQUIRE(var == test_mock.get().test_method()); }
See that the lambda returns int, while the method returns const int&. This causes segmentation fault.
int
const int&
This only happens if the method has const. In this example, a method returning int& won't compile as expected.
const
int&
Change test_method() to return an int instead of const int&?
test_method()
You shouldn't read from a reference that point to a object that has been destroyed.
If a method returns by constant reference, a Do/AlwaysDo lambda that returns by value will compile without warnings or errors:
See that the lambda returns
int
, while the method returnsconst int&
. This causes segmentation fault.This only happens if the method has
const
. In this example, a method returningint&
won't compile as expected.