【发布时间】:2019-12-14 23:02:55
【问题描述】:
我正在自学 C++20 的新概念特性,当我试图确保必须存在的成员函数只接受特定类型时遇到了障碍。我找到了this question,它向我展示了如何确保可以存在任何功能。然后我想通过确保存在具有特定参数的函数来更进一步。我首先定义T 类型必须有一个返回int 的函数number()
#include <concepts>
template <typename T>
concept MyRequirement = requires (T t)
{
{ t.number(int) } -> std::same_as<int>;
};
template <typename T>
requires MyRequirement<T>
struct RequiresBool
{
T t;
};
struct GoodType
{
int number(int x) { return x; };
};
int main()
{
RequiresBool<GoodType> good_object;
}
但是 gcc 给了我以下错误:
<source>:7:16: error: expected primary-expression before 'int'
7 | { t.number(int) } -> std::same_as<int>;
| ^~~"
所以我将其更改为以下有效。
template <typename T>
concept MyRequirement = requires (T t, int x)
{
{ t.number(x) } -> std::same_as<int>;
};
但是,当我将GoodType 中的函数签名更改为int number(double x) 时,它仍然有效。当然,double 可以隐式转换为int,这也适用于普通函数。我可以通过在GoodType 中声明(但不定义)int number(double); 来解决这个问题,这会在编译期间给我一个错误,但这让编写模板类的人有责任记住这样做。
这适用于其他类型,例如
template <typename T>
concept MyRequirement = requires (T t, std::string x)
{
{ t.number(x) } -> std::same_as<int>; //int number(int x) is now invalid
};
但是整数/双精度数让我头疼。
使用 C++20 概念,是否以某种方式确保 MyRequirement 中的 x 类型必须是整数?
【问题讨论】:
标签: c++ c++20 c++-concepts