Eme*_*pon 0 c++ c++-concepts c++20
我正在尝试实现以下概念
template<typename T>
concept GameLogic = requires(T a) {
typename T::StateType;
typename T::EventType;
{ a.initialState()->T::StateType }; // <-- relevant bit
};
Run Code Online (Sandbox Code Playgroud)
我想强制initialState()返回类型是同一类的嵌套类型。
概念定义不会引发错误(gcc 9.2),但以下实现GameLogic无法满足要求:
class SimpleGameLogic {
public:
using StateType = SimpleState;
using EventType = SimpleEvent;
StateType initialState() {
return _initialState;
}
private:
StateType _initialState;
};
Run Code Online (Sandbox Code Playgroud)
我已经尝试了上述语法的一些变体,但找不到正确的语法...或者这可能尚未实现?我究竟做错了什么?
三个问题:
{ a.initialState()->T::StateType }; // <-- relevant bit
Run Code Online (Sandbox Code Playgroud)
首先,语法错误,应该是:
{ a.initialState() } -> T::StateType;
Run Code Online (Sandbox Code Playgroud)
其次,你缺少typename:
{ a.initialState() } -> typename T::StateType;
Run Code Online (Sandbox Code Playgroud)
第三,在 C++20 中,我们不再有-> Type(请参阅此答案)。箭头右侧的东西必须是约束。就像是:
{ a.initialState() } -> std::same_as<typename T::StateType>;
Run Code Online (Sandbox Code Playgroud)
一旦你解决了这个问题,它就会起作用。
| 归档时间: |
|
| 查看次数: |
245 次 |
| 最近记录: |