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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:35:39+00:00 2026-05-22T11:35:39+00:00

I’m writing a generalized function wrapper, that can wrap any function into a lua-style

  • 0

I’m writing a generalized function wrapper, that can wrap any function into a lua-style call, which has the form

int lua_function( lua_State *L)

And I wish the wrapper function is generated on-the-fly, so I’m thinking of passing the function as a template argument. This is trivial if you know the number (e.g, 2) of arguments:

template <typename R, typename Arg1, typename Arg2, R F(Arg1, Args)>
struct wrapper

However, I don’t know the number, so I beg for variadic template argument for help

// This won't work
template <typename R, typename... Args, R F(Args...)>
struct wrapper

The above won’t compile, since variadic argument has to be the last one. So I use two level template, the outer template captures types, the inner template captures the function:

template <typename R, typename... Args>
struct func_type<R(Args...)>
{
  // Inner function wrapper take the function pointer as a template argument
  template <R F(Args...)>
  struct func
  {
    static int call( lua_State *L )
    {
      // extract arguments from L
      F(/*arguments*/);
      return 1;
    }
  };
};

That works, except that to wrap a function like

double sin(double d) {}

the user has to write

func_type<decltype(sin)>::func<sin>::apply

which is tedious.
The question is: is there any better, user-friendlier way to do it? (I can’t use a function template to wrap the whole thing, coz a function parameter can’t be used as a template argument.)

  • 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-22T11:35:40+00:00Added an answer on May 22, 2026 at 11:35 am

    Things like std::function and std::result_of use the following technique to do what you want regarding variadic templates:

    template<typename Signature>
    struct wrapper; // no base template
    
    template<typename Ret, typename... Args>
    struct wrapper<Ret(Args...)> {
        // instantiated for any function type
    };
    

    You could expand the above to add a non-type Ret(&P)(Args...) template parameter (pointers to function work just as well) but you’d still need a decltype at the user level, i.e. wrapper<decltype(sin), sin>::apply. Arguably it would be a legitimate use of the preprocessor if you decide to use a macro to remove the repetition.

    template<typename Sig, Sig& S>
    struct wrapper;
    
    template<typename Ret, typename... Args, Ret(&P)(Args...)>
    struct wrapper<Ret(Args...), P> {
        int
        static apply(lua_State*)
        {
            // pop arguments
            // Ret result = P(args...);
            // push result & return
            return 1;
        }
    };
    
    // &wrapper<decltype(sin), sin>::apply is your Lua-style wrapper function.
    

    The above compiles with gcc-4.5 at ideone.
    Good luck with implementing the apply that (variadically) pops the arguments (leave me a comment if you open a question about that). Have you considered using Luabind?

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.