zjyhjqs
zjyhjqs

Reputation: 711

Can concepts(C++20) be used as a boolean?

using T = decltype(std::same_as<int, double>) I have tried this on VS2019. It turns out that T = bool.

Is this defined by standard, or just a property by specific compilers? I haven't found any official information about it.. https://en.cppreference.com/w/cpp/concepts

Upvotes: 6

Views: 1933

Answers (1)

A concept-id like std::same_as<int, double> is evaluated like an expression. It produces a prvalue of type bool.

[temp.names]

8 A concept-id is a simple-template-id where the template-name is a concept-name. A concept-id is a prvalue of type bool, and does not name a template specialization. A concept-id evaluates to true if the concept's normalized constraint-expression is satisfied ([temp.constr.constr]) by the specified template arguments and false otherwise.

So decltype is reporting it correctly. In an expression, it's a bool.

Upvotes: 9

Related Questions