概念的主体是定义还是需要 block 未评估的上下文?例如。我可以安全地使用 std::declval
吗?
template<typename T>
concept bool SomeConcept = requires(T a) {
{ a.someFunction(std::declval<int>()) } -> int;
};
最佳答案
是的。来自 [temp.constr.expr],措辞截至 N4641 :
An expression constraint is a constraint that specifies a requirement on the formation of an expression
E
through substitution of template arguments. An expression constraint is satisfied if substitution yieldingE
did not fail. Within an expression constraint,E
is an unevaluated operand (Clause 5).
所以使用 declval
应该没问题。
或者,您可以只创建所需类型的对象,因为在需求上下文中,我们实际上并没有构建任何东西:
template<typename T>
concept bool SomeConcept = requires(T a, int i) {
{ a.someFunction(std::move(i)) } -> int;
};
关于c++ - requires 的主体是否会阻止未评估的上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43475730/