Ash*_*nxy 2 c++ struct function-pointers
查看 C++ 接口代码。我无权访问实施。我做了一个小例子来展示这种行为。
struct MessageInfo{
MessageInfo() : length{}, from{}, to{} {}
MessageInfo(int _length, string _from, string _to) : length{_length}, from{_from}, to{_to}
{}
int length;
string from;
string to;
using IsEnumerableTag = void;
template<typename F>
void enumerate(F& fun) {
fun(this->length);
fun(this->from);
fun(this->to);
}
};
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释一下这个结构定义中枚举结构函数成员的用法吗?
根据我的理解,该结构中的枚举可以采用函数类型作为输入参数(函数指针?)
MessageInfo messageInfo (1000, "A", "B");
messageInfo.enumerate<???>(printFrom(messageInfo.From);
void printFrom(string f) {
cout<<"the msgInfo is sent from "<< f<<endl;
}
Run Code Online (Sandbox Code Playgroud)
它期望您传递一个通用的可调用函数,例如 lambda。您不必指定模板参数。可以从函数参数推导出来。
例如:
MessageInfo messageInfo (1000, "A", "B");
auto printFields = [](auto&& f){ std::cout << "Value of this field is " << f << ".\n"; };
messageInfo.enumerate(printFields);
Run Code Online (Sandbox Code Playgroud)
应该打印哪个
Value of this field is 1000.
Value of this field is A.
Value of this field is B.
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,enumerate可以用于对每个成员应用相同的操作,而不必为每个成员重复操作。
签名有点不寻常。您通常会期望F或F&&代替F&. 使用 或者F,F&&您可以将 lambda 表达式直接放入调用中,而不必先将其存储在变量中。