c++ - requires 的主体是否会阻止未评估的上下文?

标签 c++ c++-concepts

概念的主体是定义还是需要 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 yielding E 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/

相关文章:

c++ - 可扩展的解析器设计

c++ - 在 GLFW 窗口标题中显示 FPS?

c++ - 将拷贝传播到Bazel中的所有依赖项

c++ - 如何强制 QDateTime::fromString 读取 UTC 时间

c++ - 为什么 output_iterator 概念不需要 output_iterator_tag?

C++ 概念 Same 和 Assignable

c++ - 没有循环的算法复杂度?

c++ - 用概念重载函数

c++ - Visual Studio 2019 C++ 对概念的支持 - 编译成功,但出现错误 : Why?

c++ - 我可以编写一个需要函数模板的 C++ 概念吗?必须向该函数模板提供一些枚举值作为模板参数?