How do I write a concept that demands that the class has a noexcept
constructor? For example, the following static_assert
is true in Clang 15.0.7, although I feel that it shouldn't.
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>);
What is the noexcept
expression doing then anyway in the concept? (Feel free to also link any good concepts tutorials)
You can check if an expression is noexcept
in a requires expression with a noexcept
before the ->
or ;
:
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>;
};