From e9ddcaf6bc819243679af4bb0d5af7846cc9245c Mon Sep 17 00:00:00 2001 From: Nia Bickford Date: Tue, 14 Jan 2025 00:29:51 -0800 Subject: [PATCH] Fix compilation on Visual Studio 2019 v16.11.42 Although Visual Studio 2022 allows function members in a templated class to use `requires` (and this appears to be permitted by the language, although I haven't dug deeper than CPPReference here), Visual Studio 2019 emits Error C7599, "a trailing requires clause is only allowed on a templated function" when attempting to compile members of `linear_memory_resource`. My solution is to make these functions templated as well, with a default template argument equal to the class' template argument. Since templated constructors can't be defaulted, I've added the default implementation there as well. Signed-off-by: Nia Bickford --- include/decodeless/allocator.hpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/include/decodeless/allocator.hpp b/include/decodeless/allocator.hpp index 7d6ea25..11d1330 100644 --- a/include/decodeless/allocator.hpp +++ b/include/decodeless/allocator.hpp @@ -55,6 +55,11 @@ class linear_memory_resource { using parent_allocator = ResOrAlloc; // Non-reallocating parent allocator constructor must take an initial size +#if _MSC_VER < 1930 + // MSVC 2019 doesn't allow a trailing requires clause on a function of a + // templated class. Adding a dummy template like this avoids error C7599. + template +#endif linear_memory_resource(size_t initialSize, const ResOrAlloc& parent = ResOrAlloc()) requires allocator : m_parent(parent) @@ -68,6 +73,9 @@ class linear_memory_resource { // Non-reallocating parent memory_resource constructor must take an initial // size +#if _MSC_VER < 1930 + template +#endif linear_memory_resource(size_t initialSize, ResOrAlloc&& parent) requires memory_resource : m_parent(std::move(parent)) @@ -80,17 +88,26 @@ class linear_memory_resource { } // Reallocating parent allocator may default construct +#if _MSC_VER < 1930 + template +#endif linear_memory_resource() requires realloc_allocator - = default; + : m_parent() {} // Reallocating parent allocator can be copied +#if _MSC_VER < 1930 + template +#endif linear_memory_resource(const ResOrAlloc& parent) requires realloc_allocator : m_parent(parent) {} // Reallocating parent memory_resource must be moved into the linear // resource +#if _MSC_VER < 1930 + template +#endif linear_memory_resource(ResOrAlloc&& parent) requires realloc_memory_resource : m_parent(std::move(parent)) {}