通用 std::atomic<T>
需要有 T
即可复制构造和可复制分配:
The program is ill-formed if any of
(1.1)
is_trivially_copyable_v<T>
,(1.2)
is_copy_constructible_v<T>
,(1.3)
is_move_constructible_v<T>
,(1.4)
is_copy_assignable_v<T>
,or (1.5)
is_move_assignable_v<T>
是false
.
以上对 C++20 来说并不陌生。编译器可以使用static_assert
为不合格的 T 发出错误。
但是,C++20 可以使用带有requires
的形式约束。正式要求上述内容作为类型一部分的语法,例如就像是:template< class T > requires std::is_trivially_copyable_v<T> && std::is_copy_constructible_v<T> && std::is_move_constructible_v<T> && std::is_copy_assignable_v<T> && std::is_move_assignable_v<T> struct atomic { ... };
C++20 是否有理由避免为此目的使用形式约束?
编辑: @T.C.正确指出,在下面的答案中:
For
std::atomic
in particular, constraining the primary template is simply not an option, given theatomic<shared_ptr<T>>
andatomic<weak_ptr<T>>
specializations that were added in C++20.
有一个选项建议:
Perhaps you can do something fancier (like an undefined and unconstrained primary template plus a constrained partial specialization), but it adds very little value.
嗯,还有另一种选择,不需要未定义和
不受约束的主模板,它仍然有点复杂,降低了使用此用法概念的值(value)和乐趣,但可能比未定义的基本模板更好:template< class T > requires std::is_trivially_copyable_v<T> && std::is_copy_constructible_v<T> && std::is_move_constructible_v<T> && std::is_copy_assignable_v<T> && std::is_move_assignable_v<T> || std::same_as<T, std::shared_ptr<typename T::element_type>> || std::same_as<T, std::weak_ptr<typename T::element_type>> struct atomic { ... }; template< class T > struct atomic<std::shared_ptr<T>> { ... }; template< class T > struct atomic<std::weak_ptr<T>> { ... }; // types of all other specializations are Copy Constructible and Copy Assignable
代码:https://godbolt.org/z/JaCu78
最佳答案
库规范故意避免使用任何特定技术来实现其目标 P0788 :
IV. Let’s avoid any specification that demands any particular technology by which implementations must comply with Library specifications.
a) Let’s permit an implementation to use a requires-clause, an
enable_if
, aconstexpr if
, or any other technology or combination of technologies to meet Constraints: specifications.b) Let’s permit an implementation to use
static_assert
and/or any other technologies to meet Mandates: specifications.c) Let’s permit an implementation to use Contracts attributes [P0542R1] and/or any other technologies to meet Expects: and Ensures: specifications.
d) Let’s consider user code that relies on any specific technology on the part of an implementation to be ill-formed, with no diagnostic required.
在 P1369 中对其进行了扩展.
目标是避免将库的规范与它的任何特定实现联系起来。在某些情况下,您确实需要这样做 - 许多范围的事情确实需要概念才能工作,因此它们以这种方式指定 - 但在大多数情况下,您不需要。
对于用户来说,重要的部分是对
T
的强制要求。 .如何执行这些要求并不重要。它可能是一个概念,也可能是一个 static_assert
,它可能是一些编译器内在的,无论如何。
关于c++ - 为什么 C++20 不使用 `requires` 来限制 atomic<T> 的 T?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62154178/