From 1e3f11ff6dce092af6157b18545b3534d7e72ac3 Mon Sep 17 00:00:00 2001 From: phosit Date: Sun, 19 Nov 2023 21:04:40 +0000 Subject: [PATCH] Disallow conversion in Future return Differential Revision: https://code.wildfiregames.com/D4812 This was SVN commit r27947. --- source/ps/Future.h | 5 +++-- source/ps/tests/test_Future.h | 16 ++++------------ 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/source/ps/Future.h b/source/ps/Future.h index 361578e357..6c56216e4e 100644 --- a/source/ps/Future.h +++ b/source/ps/Future.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -280,7 +280,8 @@ template template PackagedTask Future::Wrap(T&& func) { - static_assert(std::is_convertible_v, ResultType>, "The return type of the wrapped function cannot be converted to the type of the Future."); + static_assert(std::is_same_v, ResultType>, + "The return type of the wrapped function is not the same as the type the Future expects."); m_SharedState = std::make_shared(std::move(func)); return PackagedTask(m_SharedState); } diff --git a/source/ps/tests/test_Future.h b/source/ps/tests/test_Future.h index 46042941ab..7858601000 100644 --- a/source/ps/tests/test_Future.h +++ b/source/ps/tests/test_Future.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -54,14 +54,6 @@ public: TS_ASSERT_EQUALS(future.Get(), 1); } - // Convertible type. - { - Future future; - std::function task = future.Wrap([]() -> u8 { return 1; }); - task(); - TS_ASSERT_EQUALS(future.Get(), 1); - } - static int destroyed = 0; // No trivial constructor or destructor. Also not copiable. struct NonDef @@ -80,21 +72,21 @@ public: TS_ASSERT_EQUALS(destroyed, 0); { Future future; - std::function task = future.Wrap([]() { return 1; }); + std::function task = future.Wrap([]() { return NonDef{1}; }); task(); TS_ASSERT_EQUALS(future.Get().value, 1); } TS_ASSERT_EQUALS(destroyed, 1); { Future future; - std::function task = future.Wrap([]() { return 1; }); + std::function task = future.Wrap([]() { return NonDef{1}; }); } TS_ASSERT_EQUALS(destroyed, 1); /** * TODO: find a way to test this { Future future; - std::function task = future.Wrap([]() { return 1; }); + std::function task = future.Wrap([]() { return NonDef{1}; }); future.CancelOrWait(); TS_ASSERT_THROWS(future.Get(), const Future::BadFutureAccess&); }