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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:02:03+00:00 2026-06-01T14:02:03+00:00

I have a set of types templated on an integer N, and a class

  • 0

I have a set of types templated on an integer N, and a class to contain all the types where N <= K and N >= 0, something like

template <int N> struct T { int i; }

template <int N> struct TContainer{
    std::vector<T<N>> vec;
    TContainer<N - 1> prev;

    template <int target_n> TContainer<target_n> & get() { return prev.get(); }
    template <> TContainer<N> & get<N>() { return *this; }
};

template <> struct TContainer<0>{
    std::vector<T<N>> vec;

    template <int target_n> TContainer<target_n> & get() {  }
    template <> TContainer<0> & get<0>() { return *this; }
};

class ContainsAll{
    TContainer<K> my_data;
    int my_instance_data;
};

Is it possible for ContainsAll to have some kind of foreach function, which would be able to take a template member function to act on each element for all N? Something like

template<int N>
void for_each(TContainer<N> & container, template_function){
    for(auto it = container.vec.begin(); it != container.vec.end(); ++it){
        template_function(*it);
    }
    for_each<K - 1>(container.prev, template_function);
}

template<> void for_each<0>(TContainer<0> & container, template_function){
    for(auto it = container.vec.begin(); it != container.vec.end(); ++it){
        template_function(*it);
    }
}

So I can do

template <int N> add_to_T(T<N> & in){
    in.i += my_instance_data;
}

for_each(my_container, add_to_T);
  • 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-01T14:02:04+00:00Added an answer on June 1, 2026 at 2:02 pm

    You can’t pass a template function. But you can pass a instance of a structure (a function object), which contains a template function, e.g.

    template <typename F>
    void for_each(TContainer<0>& container, F f)
    {
        for (auto& t : container.vec)
            f(t);
    }
    
    template <int N, typename F>
    void for_each(TContainer<N>& container, F f)
    {
        for (auto& t : container.vec)
            f(t);
        for_each(container.prev, std::move(f));
    }
    
    
    struct TAdder {
        template <int N>
        void operator()(T<N>& t) const
        {
            t.i += N;
        }
    };
    
    for_each(c, TAdder{});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose I have a struct like this: struct my_struct { int a; int
I have a set of functions that are templated both by an integer type
I have a set of SQL statements formatted like so: INSERT INTO reports.workload (workload_id,event_time,event_type_id,agent_id)
I have a Map that uses a Set for the key type, like this:
I have the following class template: template<class T, unsigned N> class MyClass; where T
Hi I have a set of Calculator class that does the calculation for different
I have a collection that holds multiple types of items that all inherit from
I have the following template structure: template <typename scalar_type> struct postc_params{ scalar_type delta; unsigned
So in writing a C++ template class, I have defined a method that returns
I have a ViewModel that looks like this: public class CreateReviewViewModel { public string

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.