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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:13:24+00:00 2026-05-26T18:13:24+00:00

Let’s say you want to write a function which gets passed an opaque handle

  • 0

Let’s say you want to write a function which gets passed an opaque handle to a function of unknown type (say, the name of a struct containing a function with an agreed-upon name), and forwards arguments to that function.

In the non-variadic case, considering single-parameter functions for simplicity, there’s two ways to do this: you can let the forwarding function take an argument of arbitrary type, and attempt to call the forwardee function with it, and the compiler will complain during template expansion if it turns out to be incompatible; or you can use decltype and assorted other mechanisms to figure out what type of parameter the forwardee function expects, and explicitly require an argument of that type. I don’t know if there’s any accepted terminology for these, so I’m going to call them “pass through” and “up front”.

The pass through method generalizes straightforwardly to functions with an arbitrary number of parameters, but the up front method doesn’t.

#include <iostream>

template<typename T, typename Arg>
void pass_through_1(Arg arg)
{
    T::f(arg);
}

template<typename T> struct arg_of_1;

template<typename Ret, typename Arg>
struct arg_of_1<Ret (Arg)>
{
    typedef Arg type;
};

template<typename T>
void up_front_1(typename arg_of_1<decltype(T::f)>::type arg)
{
    T::f(arg);
}

template<typename T, typename... Args>
void pass_through_var(Args... args)
{
    T::f(args...);
}

template<typename T> struct args_of_var;

template<typename...> struct type_list;

template<typename Ret, typename... Args>
struct args_of_var<Ret (Args...)>
{
    // typedef Args... type; // can't do this
    typedef type_list<Args...> type;
};

// template<typename T>
// void up_front_var(typename args_of_var<decltype(T::f)>::type... args) // can't do this
// {
//     T::f(args...);
// }

struct test  
{ 
    static void f(int x) { std::cout << x*9 << std::endl; }
};

int main(int, char**)
{
    pass_through_1<test>(7);
    up_front_1<test>(8);
    pass_through_var<test>(9);
    // up_front_var<test>(10);
    return 0;
}

The problem is that parameter packs aren’t allowed to be free-standing, only as template arguments, and if you wrap them in an enclosing template, there’s no way to unwrap-and-unpack them in place, only by pattern matching.

“Up front” has some advantages like better self-documentation, and better support for type inference (up_front<T> can itself be decltyped). Is there any way to make it work in the variadic case? (You could of course use std::tuple, but that’s rather unsatisfying.)

  • 1 1 Answer
  • 1 View
  • 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-26T18:13:25+00:00Added an answer on May 26, 2026 at 6:13 pm

    There’s nothing like writing down the question to make you realize the answer.

    Here’s one way:

    template<typename T, typename Args = typename args_of_var<decltype(T::f)>::type>
    struct up_front_var;
    
    template<typename T, typename... Args>
    struct up_front_var<T, type_list<Args...>>
    {
        static void forward(Args... args)
        {
            T::f(args...);
        }
    };
    

    I don’t think there’s a way to make a top-level function out of this (you run into the original problem again), but that’s possibly not too bad.

    Would still be happy to see other solutions.

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

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say I have window.open (without name parameter), scattered in my project and I
Let's say after I had login I will be prompt to enter the Name
Let's say, if I need to assign an id to element with first name,
Let's say I'm making a tool to help epicureans keep track of which delicacies
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I'm writing a Windows Forms (.NET Framework 3.5) application which shows the

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.