C++ Template Function to Iterate Over any Collection Member Field2019 Community Moderator ElectionStoring C++ template function definitions in a .CPP fileHow do I iterate over the words of a string?Can a C++ class member function template be virtual?Why do we need virtual functions in C++?C++ template typedefFailed to specialize function templateC++ Template meta-magic, template call-site qualification deduction mechanismCan't correctly instantiate template when using C++ functional libraryCompiling boost::move in gcc 4.6.4Templated Function that works for iterators over raw pointers as well as iterators over unique_ptrs
Is there a place to find the pricing for things not mentioned in the PHB? (non-magical)
Recruiter wants very extensive technical details about all of my previous work
Is it good practice to use Linear Least-Squares with SMA?
Official degrees of earth’s rotation per day
Is it true that good novels will automatically sell themselves on Amazon (and so on) and there is no need for one to waste time promoting?
How to pronounce "I ♥ Huckabees"?
How do you talk to someone whose loved one is dying?
Can I use USB data pins as a power source?
What did “the good wine” (τὸν καλὸν οἶνον) mean in John 2:10?
What's the meaning of a knight fighting a snail in medieval book illustrations?
Have the tides ever turned twice on any open problem?
et qui - how do you really understand that kind of phraseology?
How should I state my peer review experience in the CV?
What is the significance behind "40 days" that often appears in the Bible?
About the actual radiative impact of greenhouse gas emission over time
When to use a slotted vs. solid turner?
Why is the President allowed to veto a cancellation of emergency powers?
Violin - Can double stops be played when the strings are not next to each other?
What does 高層ビルに何車線もの道路。mean?
How can we have a quark condensate without a quark potential?
Are all passive ability checks floors for active ability checks?
Is honey really a supersaturated solution? Does heating to un-crystalize redissolve it or melt it?
How could an airship be repaired midflight?
The German vowel “a” changes to the English “i”
C++ Template Function to Iterate Over any Collection Member Field
2019 Community Moderator ElectionStoring C++ template function definitions in a .CPP fileHow do I iterate over the words of a string?Can a C++ class member function template be virtual?Why do we need virtual functions in C++?C++ template typedefFailed to specialize function templateC++ Template meta-magic, template call-site qualification deduction mechanismCan't correctly instantiate template when using C++ functional libraryCompiling boost::move in gcc 4.6.4Templated Function that works for iterators over raw pointers as well as iterators over unique_ptrs
I am attempting to write a template function that iterates over a user-specified field within some collection of structs. For example, I want to write the following 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 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());
The above doesn't work, but hopefully the intent is clear. How can I write the myFunc template method to accomplish generic iteration over some field of each iterated item? Alternatively, if there is some way (in Boost or the Standard Library) to directly create an iterator over exArray[i].a
, that would also be acceptable.
c++ templates iterator
add a comment |
I am attempting to write a template function that iterates over a user-specified field within some collection of structs. For example, I want to write the following 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 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());
The above doesn't work, but hopefully the intent is clear. How can I write the myFunc template method to accomplish generic iteration over some field of each iterated item? Alternatively, if there is some way (in Boost or the Standard Library) to directly create an iterator over exArray[i].a
, that would also be acceptable.
c++ templates iterator
add a comment |
I am attempting to write a template function that iterates over a user-specified field within some collection of structs. For example, I want to write the following 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 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());
The above doesn't work, but hopefully the intent is clear. How can I write the myFunc template method to accomplish generic iteration over some field of each iterated item? Alternatively, if there is some way (in Boost or the Standard Library) to directly create an iterator over exArray[i].a
, that would also be acceptable.
c++ templates iterator
I am attempting to write a template function that iterates over a user-specified field within some collection of structs. For example, I want to write the following 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 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());
The above doesn't work, but hopefully the intent is clear. How can I write the myFunc template method to accomplish generic iteration over some field of each iterated item? Alternatively, if there is some way (in Boost or the Standard Library) to directly create an iterator over exArray[i].a
, that would also be acceptable.
c++ templates iterator
c++ templates iterator
edited Mar 6 at 21:07
Jeff G
asked Mar 6 at 21:01
Jeff GJeff G
1,9691744
1,9691744
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
It's simple if you know the pointer to member syntax and the likes. Unfortunately is so rarely used, is kind of an esoteric feature of the language:
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> v5, 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)
For the template <auto Field
you need C++17.
For C++11 the syntax is more verbose:
template <class T, class F, F T::* Field, class It>
void myFunc(It begin, It end)
/* same */
int main()
std::vector<Example> v5, true, 8, false;
myFunc<Example, int, &Example::a>(v.begin(), v.end()); // will call foo<int>(5) , foo<int>(8)
myFunc<Example, bool, &Example::b>(v.begin(), v.end()); // will call foo<bool>(true) , foo<bool>(false)
A little bit OT to your question, but I don't understand why you complicate yourself with that initialization of std::list
. In C++ your first container of choice should be std::vector
.
Also there is no std::iterator
I actually am usingstd::vector
currently. The reason I didn't put that in the question is because I'm guessing if I did, the first answer I got back would have assumed the first argument tomyFunc
could always beClass*
. I wanted higher quality answers, so I chose a type that requiresmyFunc
to accept both pointers and iterators.
– Jeff G
Mar 6 at 21:57
@JeffG a pointer is an iterator
– bolov
Mar 6 at 22:16
Yes, but an iterator isn't a pointer. I didn't want answers that only worked for pointers.
– Jeff G
Mar 6 at 22:23
Unfortunately, I am stuck on VS2017u2, which doesn't have auto template parameter support. However, I was able to use the declarationtemplate<typename I, typename T, typename F> void myFunc(I begin, size_t length, F T::*field);
.
– Jeff G
Mar 7 at 0:52
add a comment |
What I usually use is something like:
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));
Please notice how I used std::array
instead of a raw c style array.
If you have access to a c++11 compiler, std::array
(or std::vector
) should always be preferred over raw c arrays. ES.27
In order to need less boilerplate code, consider using some serialization libraries which solve this "iterating over class fields" problem, for example boost serialization or magic get.
This answer along with pointer to member parameters has it. You don't need lambda functions, and can simply pass &Example::a directly as the second parameter.
– Jeff G
Mar 7 at 0:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032061%2fc-template-function-to-iterate-over-any-collection-member-field%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's simple if you know the pointer to member syntax and the likes. Unfortunately is so rarely used, is kind of an esoteric feature of the language:
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> v5, 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)
For the template <auto Field
you need C++17.
For C++11 the syntax is more verbose:
template <class T, class F, F T::* Field, class It>
void myFunc(It begin, It end)
/* same */
int main()
std::vector<Example> v5, true, 8, false;
myFunc<Example, int, &Example::a>(v.begin(), v.end()); // will call foo<int>(5) , foo<int>(8)
myFunc<Example, bool, &Example::b>(v.begin(), v.end()); // will call foo<bool>(true) , foo<bool>(false)
A little bit OT to your question, but I don't understand why you complicate yourself with that initialization of std::list
. In C++ your first container of choice should be std::vector
.
Also there is no std::iterator
I actually am usingstd::vector
currently. The reason I didn't put that in the question is because I'm guessing if I did, the first answer I got back would have assumed the first argument tomyFunc
could always beClass*
. I wanted higher quality answers, so I chose a type that requiresmyFunc
to accept both pointers and iterators.
– Jeff G
Mar 6 at 21:57
@JeffG a pointer is an iterator
– bolov
Mar 6 at 22:16
Yes, but an iterator isn't a pointer. I didn't want answers that only worked for pointers.
– Jeff G
Mar 6 at 22:23
Unfortunately, I am stuck on VS2017u2, which doesn't have auto template parameter support. However, I was able to use the declarationtemplate<typename I, typename T, typename F> void myFunc(I begin, size_t length, F T::*field);
.
– Jeff G
Mar 7 at 0:52
add a comment |
It's simple if you know the pointer to member syntax and the likes. Unfortunately is so rarely used, is kind of an esoteric feature of the language:
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> v5, 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)
For the template <auto Field
you need C++17.
For C++11 the syntax is more verbose:
template <class T, class F, F T::* Field, class It>
void myFunc(It begin, It end)
/* same */
int main()
std::vector<Example> v5, true, 8, false;
myFunc<Example, int, &Example::a>(v.begin(), v.end()); // will call foo<int>(5) , foo<int>(8)
myFunc<Example, bool, &Example::b>(v.begin(), v.end()); // will call foo<bool>(true) , foo<bool>(false)
A little bit OT to your question, but I don't understand why you complicate yourself with that initialization of std::list
. In C++ your first container of choice should be std::vector
.
Also there is no std::iterator
I actually am usingstd::vector
currently. The reason I didn't put that in the question is because I'm guessing if I did, the first answer I got back would have assumed the first argument tomyFunc
could always beClass*
. I wanted higher quality answers, so I chose a type that requiresmyFunc
to accept both pointers and iterators.
– Jeff G
Mar 6 at 21:57
@JeffG a pointer is an iterator
– bolov
Mar 6 at 22:16
Yes, but an iterator isn't a pointer. I didn't want answers that only worked for pointers.
– Jeff G
Mar 6 at 22:23
Unfortunately, I am stuck on VS2017u2, which doesn't have auto template parameter support. However, I was able to use the declarationtemplate<typename I, typename T, typename F> void myFunc(I begin, size_t length, F T::*field);
.
– Jeff G
Mar 7 at 0:52
add a comment |
It's simple if you know the pointer to member syntax and the likes. Unfortunately is so rarely used, is kind of an esoteric feature of the language:
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> v5, 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)
For the template <auto Field
you need C++17.
For C++11 the syntax is more verbose:
template <class T, class F, F T::* Field, class It>
void myFunc(It begin, It end)
/* same */
int main()
std::vector<Example> v5, true, 8, false;
myFunc<Example, int, &Example::a>(v.begin(), v.end()); // will call foo<int>(5) , foo<int>(8)
myFunc<Example, bool, &Example::b>(v.begin(), v.end()); // will call foo<bool>(true) , foo<bool>(false)
A little bit OT to your question, but I don't understand why you complicate yourself with that initialization of std::list
. In C++ your first container of choice should be std::vector
.
Also there is no std::iterator
It's simple if you know the pointer to member syntax and the likes. Unfortunately is so rarely used, is kind of an esoteric feature of the language:
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> v5, 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)
For the template <auto Field
you need C++17.
For C++11 the syntax is more verbose:
template <class T, class F, F T::* Field, class It>
void myFunc(It begin, It end)
/* same */
int main()
std::vector<Example> v5, true, 8, false;
myFunc<Example, int, &Example::a>(v.begin(), v.end()); // will call foo<int>(5) , foo<int>(8)
myFunc<Example, bool, &Example::b>(v.begin(), v.end()); // will call foo<bool>(true) , foo<bool>(false)
A little bit OT to your question, but I don't understand why you complicate yourself with that initialization of std::list
. In C++ your first container of choice should be std::vector
.
Also there is no std::iterator
edited Mar 7 at 8:41
answered Mar 6 at 21:42
bolovbolov
32k673137
32k673137
I actually am usingstd::vector
currently. The reason I didn't put that in the question is because I'm guessing if I did, the first answer I got back would have assumed the first argument tomyFunc
could always beClass*
. I wanted higher quality answers, so I chose a type that requiresmyFunc
to accept both pointers and iterators.
– Jeff G
Mar 6 at 21:57
@JeffG a pointer is an iterator
– bolov
Mar 6 at 22:16
Yes, but an iterator isn't a pointer. I didn't want answers that only worked for pointers.
– Jeff G
Mar 6 at 22:23
Unfortunately, I am stuck on VS2017u2, which doesn't have auto template parameter support. However, I was able to use the declarationtemplate<typename I, typename T, typename F> void myFunc(I begin, size_t length, F T::*field);
.
– Jeff G
Mar 7 at 0:52
add a comment |
I actually am usingstd::vector
currently. The reason I didn't put that in the question is because I'm guessing if I did, the first answer I got back would have assumed the first argument tomyFunc
could always beClass*
. I wanted higher quality answers, so I chose a type that requiresmyFunc
to accept both pointers and iterators.
– Jeff G
Mar 6 at 21:57
@JeffG a pointer is an iterator
– bolov
Mar 6 at 22:16
Yes, but an iterator isn't a pointer. I didn't want answers that only worked for pointers.
– Jeff G
Mar 6 at 22:23
Unfortunately, I am stuck on VS2017u2, which doesn't have auto template parameter support. However, I was able to use the declarationtemplate<typename I, typename T, typename F> void myFunc(I begin, size_t length, F T::*field);
.
– Jeff G
Mar 7 at 0:52
I actually am using
std::vector
currently. The reason I didn't put that in the question is because I'm guessing if I did, the first answer I got back would have assumed the first argument to myFunc
could always be Class*
. I wanted higher quality answers, so I chose a type that requires myFunc
to accept both pointers and iterators.– Jeff G
Mar 6 at 21:57
I actually am using
std::vector
currently. The reason I didn't put that in the question is because I'm guessing if I did, the first answer I got back would have assumed the first argument to myFunc
could always be Class*
. I wanted higher quality answers, so I chose a type that requires myFunc
to accept both pointers and iterators.– Jeff G
Mar 6 at 21:57
@JeffG a pointer is an iterator
– bolov
Mar 6 at 22:16
@JeffG a pointer is an iterator
– bolov
Mar 6 at 22:16
Yes, but an iterator isn't a pointer. I didn't want answers that only worked for pointers.
– Jeff G
Mar 6 at 22:23
Yes, but an iterator isn't a pointer. I didn't want answers that only worked for pointers.
– Jeff G
Mar 6 at 22:23
Unfortunately, I am stuck on VS2017u2, which doesn't have auto template parameter support. However, I was able to use the declaration
template<typename I, typename T, typename F> void myFunc(I begin, size_t length, F T::*field);
.– Jeff G
Mar 7 at 0:52
Unfortunately, I am stuck on VS2017u2, which doesn't have auto template parameter support. However, I was able to use the declaration
template<typename I, typename T, typename F> void myFunc(I begin, size_t length, F T::*field);
.– Jeff G
Mar 7 at 0:52
add a comment |
What I usually use is something like:
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));
Please notice how I used std::array
instead of a raw c style array.
If you have access to a c++11 compiler, std::array
(or std::vector
) should always be preferred over raw c arrays. ES.27
In order to need less boilerplate code, consider using some serialization libraries which solve this "iterating over class fields" problem, for example boost serialization or magic get.
This answer along with pointer to member parameters has it. You don't need lambda functions, and can simply pass &Example::a directly as the second parameter.
– Jeff G
Mar 7 at 0:01
add a comment |
What I usually use is something like:
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));
Please notice how I used std::array
instead of a raw c style array.
If you have access to a c++11 compiler, std::array
(or std::vector
) should always be preferred over raw c arrays. ES.27
In order to need less boilerplate code, consider using some serialization libraries which solve this "iterating over class fields" problem, for example boost serialization or magic get.
This answer along with pointer to member parameters has it. You don't need lambda functions, and can simply pass &Example::a directly as the second parameter.
– Jeff G
Mar 7 at 0:01
add a comment |
What I usually use is something like:
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));
Please notice how I used std::array
instead of a raw c style array.
If you have access to a c++11 compiler, std::array
(or std::vector
) should always be preferred over raw c arrays. ES.27
In order to need less boilerplate code, consider using some serialization libraries which solve this "iterating over class fields" problem, for example boost serialization or magic get.
What I usually use is something like:
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));
Please notice how I used std::array
instead of a raw c style array.
If you have access to a c++11 compiler, std::array
(or std::vector
) should always be preferred over raw c arrays. ES.27
In order to need less boilerplate code, consider using some serialization libraries which solve this "iterating over class fields" problem, for example boost serialization or magic get.
edited Mar 6 at 22:17
answered Mar 6 at 21:49
Jan15Jan15
315
315
This answer along with pointer to member parameters has it. You don't need lambda functions, and can simply pass &Example::a directly as the second parameter.
– Jeff G
Mar 7 at 0:01
add a comment |
This answer along with pointer to member parameters has it. You don't need lambda functions, and can simply pass &Example::a directly as the second parameter.
– Jeff G
Mar 7 at 0:01
This answer along with pointer to member parameters has it. You don't need lambda functions, and can simply pass &Example::a directly as the second parameter.
– Jeff G
Mar 7 at 0:01
This answer along with pointer to member parameters has it. You don't need lambda functions, and can simply pass &Example::a directly as the second parameter.
– Jeff G
Mar 7 at 0:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032061%2fc-template-function-to-iterate-over-any-collection-member-field%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown