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 8777593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:18:44+00:00 2026-06-13T19:18:44+00:00

I am writing a few algorithms to build random forests, each forest will be

  • 0

I am writing a few algorithms to build random forests, each forest will be
trained on separate data with separate functions (each tree will use a set of
functions with a fixed signature however different trees will be trained using
different sets of functions which could have a different signature), however I
would like to just write the code to build the random trees once, using
templates. I currently have something like the following:

template class T corresponds to the training data type (i.e. image patch, or
pixel) template class V corresponds to the function pointer type

template<class T, class V>
class RandomTree{

    void build(RandomTreeNode<T>& current_node,
               vector<V>& functions,
               vector<T>& data) {
        ... some code that basically calls a function passing in data T
    }
}

and I create the object like so:

typedef double (*function_ptr)(TrainingDataPoint& data_point);

RandomTree<TrainingDataPoint, function_ptr> tree = ...

The problem is that, for efficiency reasons, for one of the trees I’m
building, I want the set of functions (function_ptr’s) to take in not only the
TrainingDataPoint(template type T) but a cache of data. So that my function
pointer will look like:

typedef double (*function_ptr)(TrainingDataPoint&,
                               unordered_map<string, cv::Mat>& preloaded_images);

Now the problem is, I cant think of a way to keep the RandomTree class generic
but have some function sets (template type V) that take more than just the
training point (template type T).

So far I have thought of:

  1. Making the cache global so that the functions can access it
  2. adding a pointer to the cache to each training data point (but who is responsible for the clean up?)
  3. Adding a third template parameter to the RandomTree, but in this case if I am building a tree that doesn’t require this third parameter, what do I put there?

None of these options seem particularly appealing to me, hopefully someone can lend some experience and tell me of a better way?

Thanks

  • 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-13T19:18:46+00:00Added an answer on June 13, 2026 at 7:18 pm

    Use a functor for the functions that need state. A functor in C++ is a class (or struct) with an overloaded operator(), so that an instance of the functor can be “called like” a function. The arguments to the functor in the RandomTree should be exactly those parameters that vary and are under the control of the RandomTree, the rest should be bound outside. A sample functor with additional state that wraps a function:

    template<typename Retval, typename Arg1, typename ExtraData>
    struct BindExtraData
    {
      typedef Retval(*func_type)(Arg1, ExtraData);
      BindExtraData( ExtraData const& d_, func_type func_ ):d(d_), func(func_) {};
      ExtraData d;
      func_type func;
      Retval operator()( Arg1 a1 )
      {
        return func(a1, d);
      }
    };
    

    but you can do better. If this is a one-off, there is no need to make it a template. bind2nd(well, binder2nd) is the standard library version of the above, and will be better written.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program in C# that will need to store a few Data
I am writing a library which uses a few functions from the windows user32.dll
I am trying to speeden up my Matlab program by writing a few functions
I am writing a few extensions to mimic the map and reduce functions in
So I'm tasked with writing a few SQL queries that involve finding the highest
I am new to lisp and am writing a few simple programs to get
Short of writing a function manually that translates a few known REFIID to names,
I've completed writing a purpose-specific plugin and there are a few spots where I
I'm writing a MVVM app and have started putting in a few animations. I
I'm writing some code which involves running a few shell commands from Python and

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.