C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Helper class to make safe a thread-unsafe class

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Thu, 29 Jun 2023 08:21:19 +0100
On Wed, 28 Jun 2023 at 23:37, Frederick Virchanza Gotham via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> A few months ago I was thinking of submitting this to Boost, but now
> I'm actually thinking maybe it should be in the Standard Library.
>

It won't be approved by the Parallelism & Concurrency study group, for the
reasons already explained previously.

Maybe you should propose it for Boost though. The review might be useful.



>
>
> #if (__cplusplus >= 201700L) || (defined(__has_cpp_attribute) &&
> __has_cpp_attribute(nodiscard))
>

This is not how you test for attributes. For a start, you're assuming that
if __cplusplus >= 201703 is true then the attribute is supported, which is
not correct. The point of __has_cpp_attribute is to check if the attribute
is supported, so that's what you should use. More importantly, if
__has_cpp_attribute is not defined then this line will give an error. Using
an undefined function-like macro is ill-formed.

You need to do:
#ifdef __has_cpp_attribute
# if __has_cpp_attribute(nodiscard)
# define attrib_nodiscard [[nodiscard]]
# endif
#endif
#ifndef attrib_nodiscard
# define attrib_nodiscard [[nodiscard]]
#endif




> # define attrib_nodiscard [[nodiscard]]
> #else
> # define attrib_nodiscard /* nothing */
> #endif
>
> #if (__cplusplus >= 201907L) && defined(__cpp_concepts)
>

This is not how you use feature test macros. You're supposed to test the
value of the macro itself, not __cplusplus, so:

#if __cpp_concepts >= 201907L

Your version would give the wrong answer for a compiler with partial C++20
support, that only supports an older implementation of concepts.

Received on 2023-06-29 07:21:34