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

  • SEARCH
  • Home
  • 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 7411255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:21:34+00:00 2026-05-29T06:21:34+00:00

I have the following code defining a Functor template and the function running_op, which

  • 0

I have the following code defining a Functor template and the function
running_op, which takes an array, it’s length and a functor to apply
to the list:

  template <class Type>
  struct SumFunctor {
    Type sum;
    SumFunctor() : sum(0) {};
    Type operator()(Type next) {
      return sum += next;
    }
  };

  template <class Container, class Functor>
  inline Container running_op(Container& container, Functor functor) {
    transform(container.begin(), container.end(), container.begin(), functor);
    return container;  
  }

This is used in the following way:

  list<float> a({1,1,1,1});
  running_op(a, SumFunctor<float>());

What I would like to be able to do to avoid having type the name of
the container in the instantiated functor is use it as so:

  list<float> a({1,1,1,1});
  running_op(a, SumFunctor);

Since the contained type of a can be found in the running_op template
using Container::value_type I would like to do something as follows
(which does not work) to instantiate the functor of appropriate type:

  template <class Container, class Functor>
  inline Container running_op(Container& container, Functor functor) {
    typedef typename Container::value_type ContainerType;
    transform(container.begin(), container.end(), container.begin(), functor<ContainerType>());
    return container;
  }

Is there any way that I can pass an
uninstantiated template to another template for later instantiation?
Is there a special keyword I should use other than class in the
template parameter list (template did not work in this case)? Really
I just want to pass in a symbol which is the functor template name; is
that possible?

  • 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-05-29T06:21:35+00:00Added an answer on May 29, 2026 at 6:21 am

    You can use template templates.

      template <template <class> class Functor, class Container>
      inline Container running_op(Container& container) {
        typedef typename Container::value_type ContainerType;
        transform(container.begin(), container.end(), container.begin(), functor<ContainerType>());
        return container;
      }
    
    
      running_op<sum_function>(a);
    

    If you want that certain syntax you can use macros, although I don’t recommend it.

    #define running_op(a, b) running_op_<b>(a)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, with the intention of defining and using a list
i have the following code defining the gui of my app class Ui (object):
I have following Code Block Which I tried to optimize in the Optimized section
I have following code class User attr_accessor :name end u = User.new u.name =
I have following code snippet that i use to compile class at the run
Let's say we have the following piece of code: public class Event { }
I have code that boils down to the following: template <typename T> struct Foo
I have following code: class EntityBase (object) : __entity__ = None def __init__ (self)
I have the following code: template <typename A, typename B> struct TheStruct { A
I have the following JS code: var Item = function () { this.property =

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.