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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:39:15+00:00 2026-05-28T04:39:15+00:00

I’d like to write a function template, apply , which receives some function f

  • 0

I’d like to write a function template, apply, which receives some function f, an integer i, and a parameter pack. apply needs to unpack the parameters and apply f to them, except for the ith parameter, pi. For pi, it needs to call some other function g before passing it as a parameter to f.

It seems that I need a way to partition the parameter pack into a left side, the ith parameter, and the right side. Is this possible? In code:

template<int i, typename Function, typename... Parms>
  void apply(Function f, Parms... parms)
{
  auto lhs = // what goes here?
  auto pi =  // what goes here?
  auto rhs = // what goes here?

  f(lhs..., g(pi), rhs...);
}
  • 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-28T04:39:16+00:00Added an answer on May 28, 2026 at 4:39 am

    OK, here we go! It really ugly but I couldn’t come up with a nicer version in a hurry 😉 Most of the stuff is bog standard template specialization. The biggest issue is creating a list of integers of the proper size. I seem to recall that I came up with a nice version but somehow I can’t recall what I did. Enjoy!

    #include <iostream>
    #include <utility>
    
    // printing the values
    void print_args() {}
    template <typename F> void print_args(F f) { std::cout << f; }
    template <typename F, typename... T>
    void print_args(F f, T... args)
    {
        std::cout << f << ", ";
        print_args(args...);
    }
    
    // the function object to be called:
    struct Functor
    {
        template <typename... T>
        void operator()(T... args)
        {
            std::cout << "f(";
            print_args(args...);
            std::cout << ")\n";
        }
    };
    
    // conditionally apply g():
    template <typename T> T g(T value) { return 1000 + value; }
    template <int i, int j, typename T>
    typename std::enable_if<i != j, T>::type forward(T t) { return t; }
    template <int i, int j, typename T>
    typename std::enable_if<i == j, T>::type forward(T t) { return g(t); }
    
    // create a series of integers:
    template <int... Values> struct values {};
    
    template <int Add, typename> struct combine_values;
    template <int Add, int... Values>
    struct combine_values<Add, values<Values...>>
    {
        typedef values<Values..., Add> type;
    };
    
    template <int Size> struct make_values;
    template <> struct make_values<0> { typedef values<> type; };
    template <int Size>
    struct make_values
    {
        typedef typename combine_values<Size, typename make_values<Size -1>::type>::type type;
    };
    
    // applying f(t...) except for ti where g(ti) is called
    template <int i, int... Values, typename Function, typename... T>
    void apply_aux(values<Values...>, Function f, T... t)
    {
        f(forward<i, Values>(t)...);
    }
    
    template <int i, typename Function, typename... T>
    void apply(Function f, T... t)
    {
        apply_aux<i>(typename make_values<sizeof...(T)>::type(), f, t...);
    }
    
    int main()
    {
        apply<3>(Functor(), 1, 2, 3, 4, 5, 6, 7, 8);
        apply<4>(Functor(), 1, 2, 3, 4, 5, 6, 7, 8);
        apply<5>(Functor(), 1, 2, 3, 4, 5, 6, 7, 8);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have some data like this: 1 2 3 4 5 9 2 6
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT

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.