【发布时间】:2020-10-11 19:19:30
【问题描述】:
我可以创建一个概念来检查是否存在名为 to_string() 的成员函数,该函数在结构/类中返回 std::string。
template<typename T>
concept method_to_string = requires(T v)
{
{ v.to_string() } -> std::same_as<std::string>;
};
我想创建一个概念来检查是否存在名为 to_string 的全局函数,该函数接受 T,并返回 std::string。我做不到。我使用 gcc 10.2 作为编译器。
我的猜测是我会看起来像这样,但它失败了:
template<typename T>
concept function_to_string = requires(T v)
{
to_string(const T&) -> std::same_as<std::string>;
};
【问题讨论】:
-
这个全局函数的存在依赖什么?
-
@cigien:为什么那个函数的存在应该依赖于其他东西?
-
@einpoklum 概念约束类型。我不确定这里有什么限制。
-
好的,我明白了,
T是一个参数。我稍微编辑了这个问题,希望没问题。
标签: c++ c++20 c++-concepts