【发布时间】:2022-01-19 17:39:09
【问题描述】:
我正在学习 C++20 概念。有没有办法在auto 之前就地结合概念?例如,如果我有一个MutableGraph<G> 概念和一个VertexListGraph<G> 概念,我可以定义
template <typename G>
concept MutableVertexListGraph = MutableGraph<G> && VertexListGraph<G>;
然后做
MutableVertexListGraph auto g = ...;
但是当我希望它为这两个概念建模时,命名这个概念很烦人。如果我能做到就好了
MutableGraph && VertexListGraph auto g = ...; // && not allowed here.
甚至是类似的东西
template <typename T, concept... Concepts> // <- "concept" not allowed here.
concept ModelsAll = (Concepts<T> && ...);
...
ModelsAll<MutableGraph, VertexListGraph> auto g = ...;
我当然可以
MutableGraph auto g = ...;
requires { MutableGraph<decltype(g)>; };
缺乏对称性,或
auto g = ...;
requires { MutableGraph<decltype(g)>; MutableGraph<decltype(g)>; };
在声明行中缺少概念。
【问题讨论】:
-
我不明白这个问题。你是在问你是否可以做到(我很确定你知道你不能),或者你为什么不能做到?
-
我在问是否有办法做这样的事情,因为我猜测的东西似乎没有用。我怀疑答案是否定的,但是对于概念上可以做和不能做的各种事情,我不确定。 (或者,我想知道这是否是未来标准中可能需要的那种东西,比如
MutableGraph && VertexListGraph auto g = ...;或(MutableGraph && VertexListGraph) auto g = ...;。) -
你没有提到
template< class G > requires (MutableGraph<G> and VertexListGraph<G>) T g = ...。是不是打字太多了?
标签: c++ c++20 c++-concepts