我需要填写一些模板魔术来使以下代码片段起作用.
问题是我希望能够为std::variant接受两个参数的命名静态方法定义一个访问者类.如何填写Applicator::apply()调度工作?
struct EventA {};
struct EventB {};
struct EventC {};
using Event = std::variant<EventA, EventB, EventC>;
struct Visitor {
enum class LastEvent { None, A, B, C };
struct State {
LastEvent last_event = LastEvent::None;
};
static State apply(State s, EventA e) { return State{LastEvent::A}; }
static State apply(State s, EventB e) { return State{LastEvent::B}; }
};
template <typename Visitor> struct Applicator {
static State apply(State s, Event e) {
/*** Start of pseudo code …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚函数参数的隐式实例化是如何工作的.由于某些原因,在第一个示例中,bar()调用将其解释{12,41}为初始化列表.如果我改变了foo对签名auto foo(std::pair<int,int> bar)的{12,41}隐式转换的std::pair<int,int>.
例如
template<typename T, typename U>
auto foo(std::pair<T,U> arg) { return arg; }
auto bar() { return foo({12,41}); }
Run Code Online (Sandbox Code Playgroud)
失败并出错:
<source>: In function 'auto bar()':
104 : <source>:104:50: error: no matching function for call to 'foo(<brace-enclosed initializer list>)'
auto bar() { return foo({12,41}); }
^
103 : <source>:103:6: note: candidate: 'template<class T, class U> auto foo(std::pair<_T1, _T2>)'
auto foo(std::pair<T,U> arg) { return arg; }
^~~
103 …Run Code Online (Sandbox Code Playgroud)