Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Generalizing a function template
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Jerry Coffin, post: 3410597"] [i][i] As long as you only want to support pointers to functions (not functors) it's pretty simple -- just templatize the input and output types: // warning: only minimally tested // // This depends on 'array; defining size_type, though size_t would also // work better than int without that requirement. // template <class A, class B> array<A> apply(const array<B> &source, A (*fun)(B)) { array<A> result(source.size()); for (array<A>::size_type i=0; i<source.size(); ++i) result[i] = fun(source[i]); return result; } You'll have to work a bit harder if you want to support functors. OTOH, std::transform already provides the same basic capability with slightly differetnt syntax and more flexibility, so I don't see much point in working much more on this...[/i][/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Generalizing a function template
Top