Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7885661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:07:43+00:00 2026-06-03T05:07:43+00:00

My goal was to have a template function that would take as an input

  • 0

My goal was to have a template function that would take as an input a std::vector of input objects and a function object. This template function would then turn the input vector into a std::vector of converted objects by using the function object and a threadpool.

Example code included below.

I would really like to be able to use a shorter syntax than first creating a local function object, and then passing on all the template parameters.

Compile with gcc: g++ -std=C++0x bla.cpp

#include <vector>
#include <functional> 
#include <iostream>

// SYNTAX:
// vector<ResultType> transformed = multiTransform(const vector<InputType>, Transform t)
// where Transform t takes a single InputType as an argument
// ConvertedType has to be default constructible
template <class ConvertedType, class InputType, class Transform>
std::vector<ConvertedType> multiTransform(const std::vector<InputType>& inputs, Transform t) {
  std::vector<ConvertedType> results(inputs.size());
  {
    // boost::threadpool::pool pool(boost::thread::hardware_concurrency());
    for(auto it = inputs.begin(); it != inputs.end(); ++it){
      auto inputDereferenced = *it;
      auto functor = [&, it, inputDereferenced](){
        auto result = t(inputDereferenced);
        results[it - inputs.begin()] = std::move(result);
      };
      // pool.schedule(functor);
      functor();
    }
  }
  return results;
}

int main() {
  std::vector<int> input = {1,2,3};
  // auto output = multiTransform(input, [](int a){return float(a);}); // does not compile
  auto lambda = [](int a){return a/2.0;};
  auto output = multiTransform<float, int, decltype(lambda)>(input, lambda);

  for(auto it : output){
    std::cout << it << std::endl;
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:07:44+00:00Added an answer on June 3, 2026 at 5:07 am

    This works for me with g++ 4.6.3:

    auto output = multiTransform<float>(input, [](int a){return a/2.0;});
    

    You can also make your template declaration a bit more sophisticated:

    template <class InputType, class Transform>
    auto
    multiTransform(const std::vector<InputType>& inputs, Transform t)
      -> std::vector<decltype(t(*inputs.begin())) >
    {
      typedef decltype(t(*inputs.begin())) ConvertedType;
      std::vector<ConvertedType> results(inputs.size());
      {
        // boost::threadpool::pool pool(boost::thread::hardware_concurrency());
        for(auto it = inputs.begin(); it != inputs.end(); ++it){
          auto inputDereferenced = *it;
          auto functor = [&, it, inputDereferenced](){
            auto result = t(inputDereferenced);
            results[it - inputs.begin()] = std::move(result);
          };
          // pool.schedule(functor);
          functor();
        }
      }
      return results;
    }
    

    then you can use

    auto output = multiTransform(input, [](int a){return a/2.0;});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought that I wanted to template function specialization, but this stackoverflow article makes
Let me first describe my goal: I have created an object with 3 properties:
My goal is to have the url routing as following: http://www.abc.com/this-is-peter-page http://www.abc.com/this-is-john-page What is
My goal is to have an AsyncTask that can execute multiple times (one task
My ultimate goal is to have a menu that adds a class to the
Suppose I have an autolocker class which looks something like this: template <T> class
I have such template functions: template<class R> list<R> f(const boost::function<R()>&); template<class R, class A0>
i have a problem with this class. the goal is to make the main
I have a subset of a pointer class that look like: template <typename T>
I have an event that binds a function to a click. The click calls

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.