C++ C++;用于迭代任何集合成员字段的模板函数
我试图编写一个模板函数,该函数在某个结构集合中迭代用户指定的字段。例如,我想编写以下C++:C++ C++;用于迭代任何集合成员字段的模板函数,c++,templates,iterator,C++,Templates,Iterator,我试图编写一个模板函数,该函数在某个结构集合中迭代用户指定的字段。例如,我想编写以下C++: struct Example { int a; bool b; }; template<std::function<Field& (Class)> GetField, typename Field, typename Class> void myFunc(std::iterator<Class> begin, size_t const len
struct Example {
int a;
bool b;
};
template<std::function<Field& (Class)> GetField, typename Field, typename Class>
void myFunc(std::iterator<Class> begin, size_t const length) {
cout << length << endl;
for (size_t i{ 0 }; i < length; ++begin, ++i) {
Field const &field{ GetField(*begin) };
// Forward field to some other template function
anotherTemplateFunction<Field>(field);
}
}
void main() {
Example exArray[]{ {5, true}, {8, false} };
std::list<Example> exList{ exArray, exArray + _countof(exArray) }
// Examples of how I would like to call myFunc...
myFunc<Example::a>(exArray, _countof(exArray));
myFunc<Example::b>(exList.begin(), exList.size());
}
struct示例{
INTA;
布尔b;
};
模板
void myFunc(标准::迭代器开始,大小\u t常量长度){
cout如果您知道指向成员语法等的指针,这很简单。不幸的是,它很少使用,这是该语言的一种深奥特性:
template <class T> void foo(T);
template <auto Field, class It>
auto myFunc(It begin, It end)
{
for (; begin != end; ++begin)
{
foo((*begin).*Field);
}
}
int main()
{
std::vector<Example> v{{5, true}, {8, false}};
myFunc<&Example::a>(v.begin(), v.end()); // will call foo<int>(5) , foo<int>(8)
myFunc<&Example::b>(v.begin(), v.end()); // will call foo<bool>(true) , foo<bool>(false)
}
模板void foo(T);
模板
自动myFunc(开始、结束)
{
for(;begin!=end;++begin)
{
foo((*开始)。*字段);
}
}
int main()
{
向量v{{5,真},{8,假};
myFunc(v.begin(),v.end());//将调用foo(5),foo(8)
myFunc(v.begin(),v.end());//将调用foo(true),foo(false)
}
对于模板我通常使用的是:
void main() {
std::array<Example, 2> exArray{ {5, true}, {8, false} };
std::list<Example> exList{ exArray.begin(), exArray.end() };
auto access_a = [](Example& e)->int&{ return e.a;};
auto access_b = [](Example& e)->bool&{ return e.b;};
myFunc(exArray.begin(), exArray.end(), access_a);
myFunc(exList.begin(), exList.end(), access_b);
}
template<class ForwardIt, class Accessor>
void myFunc(ForwardIt begin,ForwardIt end, Accessor accessor) {
cout << end - begin << endl;
for (auto it = begin; it != end; it++) {
// Forward field to some other template function
anotherTemplateFunction(accessor(*it));
}
}
void main(){
数组exArray{{5,true},{8,false};
std::list exList{exArray.begin(),exArray.end()};
自动访问_a=[](示例&e)->int&{returne e.a;};
自动访问_b=[](示例&e)->bool&{return e.b;};
myFunc(exArray.begin(),exArray.end(),access_a);
myFunc(exList.begin(),exList.end(),access_b);
}
模板
void myFunc(ForwardIt begin、ForwardIt end、Accessor Accessor){
我现在真的可以使用std::vector
吗?我没有把它放在问题中的原因是因为我猜如果我这样做了,我得到的第一个答案会假设myFunc
的第一个参数总是Class*
。我想要更高质量的答案,所以我选择了一个需要myFunc的类型de>接受指针和迭代器。是的,但迭代器不是指针。我不想要只对指针有效的答案。不幸的是,我被困在VS2017u2上,它不支持自动模板参数。但是,我能够使用声明template void myFunc(我开始,size\t length,F t:*字段)
。这个答案和指向成员参数的指针都有。您不需要lambda函数,只需直接将&Example::a作为第二个参数传递即可。
void main() {
std::array<Example, 2> exArray{ {5, true}, {8, false} };
std::list<Example> exList{ exArray.begin(), exArray.end() };
auto access_a = [](Example& e)->int&{ return e.a;};
auto access_b = [](Example& e)->bool&{ return e.b;};
myFunc(exArray.begin(), exArray.end(), access_a);
myFunc(exList.begin(), exList.end(), access_b);
}
template<class ForwardIt, class Accessor>
void myFunc(ForwardIt begin,ForwardIt end, Accessor accessor) {
cout << end - begin << endl;
for (auto it = begin; it != end; it++) {
// Forward field to some other template function
anotherTemplateFunction(accessor(*it));
}
}