模板中的嵌套概念类型
考虑以下模板,旨在声明某些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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数类型都暴露了他们的“亚型”。
std :: map
haskey_type
和示例
例如。(如果需要,您可以创建特征以提取模板参数,如果类型不提供此类
typedef
)。然后,您可能会对这些子类型使用约束,例如:
Most types expose their "subtypes".
std::map
haskey_type
andmapped_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: