模板中的嵌套概念类型

发布于 2025-02-11 15:34:00 字数 1537 浏览 1 评论 0原文

考虑以下模板,旨在声明某些state :: Machine

enum Strategy {
   Breadth,
   Depth,
   Heuristic,
};

template<class Map, Strategy Strategy = Depth>
struct Machine;

如果我们想对MAP类型执行一些约束,以便实现满足必要的概念,我们就有:

template <Range::Type Source, Comparable Constraint, class Result, Transition<Source, Constraint, Result> Transition, Range::Of<Transition> Transitions, Map<Constraint, Transitions> Map, Strategy Strategy = Strategy::Depth>
class Machine {

};

whye source是某些容器 suk std :: vector约束是一些可比的struct ====结构 - &GT; bool过渡是一些lambda(source) - &gt; product过渡transition的某些容器

现在宣布这种类型正在筋疲力尽:

auto transition = [](std::string source) {
    return State::Product<std::string, State, State> {
            .source = source,
            .state = State::Start,
            .product = State::Start,
    };
};
static_assert(Transition<decltype(transition), std::string, State, State>);

std::map<State, std::set<decltype(transition)>> map = {
        {State::Start, {transition}}
};

State::Machine<std::string, State, State, decltype(transition), std::set<decltype(transition)>, decltype(map)> machine;

有没有办法让所有概念都需要使用一个类型的参数

State::Machine<decltype(map)> machine;

类似于在需要任何概念之前的方式?

Consider the following template, intended on declaring some State::Machine:

enum Strategy {
   Breadth,
   Depth,
   Heuristic,
};

template<class Map, Strategy Strategy = Depth>
struct Machine;

If we would like to enforce some constraints on the Map type, so that implementations satisfy necessary concepts, we have:

template <Range::Type Source, Comparable Constraint, class Result, Transition<Source, Constraint, Result> Transition, Range::Of<Transition> Transitions, Map<Constraint, Transitions> Map, Strategy Strategy = Strategy::Depth>
class Machine {

};

Where Source is some container like std::vector, Constraint is some comparable struct == struct -> bool, Transition is some lambda(Source) -> Product and Transitions is some container of Transition.

Declaring this type is now exhausting:

auto transition = [](std::string source) {
    return State::Product<std::string, State, State> {
            .source = source,
            .state = State::Start,
            .product = State::Start,
    };
};
static_assert(Transition<decltype(transition), std::string, State, State>);

std::map<State, std::set<decltype(transition)>> map = {
        {State::Start, {transition}}
};

State::Machine<std::string, State, State, decltype(transition), std::set<decltype(transition)>, decltype(map)> machine;

Is there a way to have all the concepts be required with only one type parameter

State::Machine<decltype(map)> machine;

Similar to how it was before any concepts were required?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2025-02-18 15:34:00

大多数类型都暴露了他们的“亚型”。
std :: map has key_type示例例如。

(如果需要,您可以创建特征以提取模板参数,如果类型不提供此类typedef)。

然后,您可能会对这些子类型使用约束,例如:

template <typename Map, Strategy Strategy = Strategy::Depth>
requires(Comparable<typename Map::key_type>
     && IsATransition<typename Map::mapped_type>)
class Machine
{
    // ...
};

Most types expose their "subtypes".
std::map has key_type and mapped_type for example.

(You can create traits to extract template parameter if needed BTW, if type doesn't provide such typedef).

Then you might use constraint on those sub-types, something like:

template <typename Map, Strategy Strategy = Strategy::Depth>
requires(Comparable<typename Map::key_type>
     && IsATransition<typename Map::mapped_type>)
class Machine
{
    // ...
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文