summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMÃ¥rten Nordheim <marten.nordheim@qt.io>2022-06-20 11:07:21 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-06-21 21:59:38 +0000
commita3cdb8632fa12ef25900a2fe0a2ba0ee49ecad17 (patch)
tree2ee464ad8ccdf9115cd997dc8ca37e52fb989f1b /tests
parent7666b58b6b43c9da9472307ceda0094c4d6c122c (diff)
function_ref test: Don't take reference of temporaries
When assigning the lambdas directly to a function_ref their lifetime is limited to that of the expression. Store them on the stack first to avoid the UB. Fixes: QTBUG-104419 Change-Id: I3c85ac683b0bd7768b646dc9d0a1ed4dd173e6f3 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> Reviewed-by: Marc Mutz <marc.mutz@qt.io> (cherry picked from commit 3d73aa660b5e1af5758ae7207ce1c05d7a0458e1) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/global/qxp/function_ref/tst_qxp_function_ref.cpp15
1 files changed, 10 insertions, 5 deletions
diff --git a/tests/auto/corelib/global/qxp/function_ref/tst_qxp_function_ref.cpp b/tests/auto/corelib/global/qxp/function_ref/tst_qxp_function_ref.cpp
index be83e76f18..6264e6a5ae 100644
--- a/tests/auto/corelib/global/qxp/function_ref/tst_qxp_function_ref.cpp
+++ b/tests/auto/corelib/global/qxp/function_ref/tst_qxp_function_ref.cpp
@@ -221,16 +221,20 @@ void tst_qxp_function_ref::voidReturning()
bool ok = false; // prevent lambdas from decaying to function pointers
{
- Fi fi = [&](int i) noexcept { return i + int(ok); };
+ auto lambda1 = [&](int i) noexcept { return i + int(ok); };
+ Fi fi = lambda1;
fi(42);
- Fv fv = [&]() noexcept { return int(ok); };
+ auto lambda2 = [&]() noexcept { return int(ok); };
+ Fv fv = lambda2;
fv();
}
{
- Fi fi = [&](int i) { return i + int(ok); };
+ auto lambda1 = [&](int i) { return i + int(ok); };
+ Fi fi = lambda1;
fi(42);
- Fv fv = [&]() { return int(ok); };
+ auto lambda2 = [&]() { return int(ok); };
+ Fv fv = lambda2;
fv();
}
}
@@ -261,7 +265,8 @@ void tst_qxp_function_ref::ctad()
#if 0 // no deduction guides for the non-function-pointer case, so no CTAD for lambdas
{
- qxp::function_ref f = [](int i) -> int { return i; };
+ auto lambda = [](int i) -> int { return i; };
+ qxp::function_ref f = lambda;
static_assert(std::is_same_v<decltype(f),
qxp::function_ref<int(int)>>);
}