Tob*_*ull 1 c++ noexcept c++-concepts c++20
如何编写一个要求类具有noexcept构造函数的概念?例如,以下内容static_assert在 Clang 15.0.7 中是正确的,尽管我觉得不应该。
class Ragdoll {
int age_ = -1;
public:
Ragdoll(int age) /* noexcept */ : age_(age) {}
int meow() const;
int lose_hair();
};
template<typename Cat>
concept cat = requires(Cat cat) {
noexcept(Cat{42});
{ cat.meow() } -> std::same_as<int>;
};
static_assert(cat<Ragdoll>);
Run Code Online (Sandbox Code Playgroud)
noexcept那么这个表达式在概念中到底在做什么呢?(也请随意链接任何好的概念教程)
您可以检查表达式是否noexcept在 require 表达式中,并在ornoexcept之前添加:->;
template<typename Cat>
concept cat = requires(const Cat cat) {
{ Cat{42} } noexcept;
// If you wanted the member function to be noexcept too for example
{ cat.meow() } noexcept -> std::same_as<int>;
};
Run Code Online (Sandbox Code Playgroud)