如何定义任意 std::vector 满足的概念? [英] How can I define a concept that is satisfied by an arbitrary std::vector?

查看:33
本文介绍了如何定义任意 std::vector 满足的概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个概念,需要一个任意向量作为返回类型:

template概念 HasVector = 需要 (T t) {{ T.vec() } ->std::same_as<std::vector<int>>;//作品{ T.vec() } ->std::same_as<std::vector<foo>>;//想在这里放一些任意的东西}

这样我们就会有如下内容:

class A {std::vectorvec() {/* ... */}}B类{std::vectorvec() {/* ... */}}static_assert(HasVector);static_assert(HasVector);

此外,需要一个向量作为返回类型会更好,其值类型满足其他一些概念,即

<预><代码>模板概念算术 =//与标准一样模板概念 HasArithmeticVector = 需要 (T t ) {{ T.vec() } ->std::same_as<std::vector<算术>>;

有没有办法把它放在概念的名称中?

解决方案

我们首先编写一个变量模板来检查一个类型是否专用于一个模板:

template Z级>内联 constexpr bool is_specialization_of = false;模板<模板<类型名称...>Z类,类... Args>内联 constexpr bool is_specialization_of, Z>= 真;

我们可以将其转化为一个概念:

template Z级>概念专业化 = is_specialization_of;

然后我们可以用它来实现另一个概念:

template概念 HasVector = 需要 (T t) {{ t.vec() } ->专门化<std::vector>;};

如果你想再做进一步的检查,那只是增加更多的要求.

template概念 HasVector = 需要 (T t) {{ t.vec() } ->专门化<std::vector>;//或类似的东西需要算术;需要算术<range_value_t<decltype(t.vec())>>;//等等.};

I would like to have a concept requiring an arbitrary vector as the return type:

template<typename T>
concept HasVector = requires (T t) {
    { T.vec() } -> std::same_as<std::vector<int>>; //works
    { T.vec() } -> std::same_as<std::vector<foo>>; //want to put something arbitrary in here
}

Such that we would have something like the following:

class A {
std::vector<int> vec() { /* ... */}
}

class B {
std::vector<double> vec() { /* ... */}
}

static_assert(HasVector<A>);
static_assert(HasVector<B>);

Moreover, it would be even nicer to require a vector as the return type whose value type satisfies some other concept, i.e.


template<typename T>
concept Arithmetic = // as in the standard

template<typename T>
concept HasArithmeticVector = requires (T t ) {
    { T. vec() } -> std::same_as<std::vector<Arithmetic>>;

Is there such a way to put this in names of concepts?

解决方案

We start by writing a variable template to check if a type specializes a template:

template <typename T, template <typename...> class Z>
inline constexpr bool is_specialization_of = false;

template <template <typename...> class Z, class... Args>
inline constexpr bool is_specialization_of<Z<Args...>, Z> = true;

Which we can turn into a concept:

template <typename T, template <typename...> class Z>
concept Specializes = is_specialization_of<T, Z>;

Which we can then use to implement another concept:

template<typename T>
concept HasVector = requires (T t) {
    { t.vec() } -> Specializes<std::vector>;
};

If you want to then do further checking, that's just adding more requirements.

template<typename T>
concept HasVector = requires (T t) {
    { t.vec() } -> Specializes<std::vector>;

    // or something along these lines
    requires Arithmetic<decay_t<decltype(t.vec()[0])>>;
    requires Arithmetic<range_value_t<decltype(t.vec())>>;
    // etc.
};

这篇关于如何定义任意 std::vector 满足的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆