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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:10:15+00:00 2026-06-09T21:10:15+00:00

For my current project, I’ve been writing a lot of C/C++ to Lua wrappers.

  • 0

For my current project, I’ve been writing a lot of C/C++ to Lua wrappers. A large number of these are simple setters and getters, so I managed to write some templates that make it easy to generate these, like so:

//        Class       Return      Field
template <typename T, typename U, U T::*Member>
int luaU_get(lua_State* L)
{
    T* obj = luaW_check<T>(L, 1);
    luaU_push<U>(L, obj->*Member);
    return 1;
}

static luaL_reg Foo_Table[] =
{
    ...
    // Now I can just use this generic template to avoid
    // writing simple getter functions
    { "getbar", luaU_get<Foo, Bar, &Foo::bar> }, 
    ...
};

I would like to do something similar for simple function wrappers for arbitrary functions too. For example, it would be nice to be able to do this:

template <typename T, typename U, U (T::*Func)(), typename... Args>
int luaU_func(lua_State* L)
{
     // ...?
}

static luaL_reg Foo_Table[] =
{
    ...
    { "baz", luaU_func<Foo, int, &Foo::baz, int, float> }, 
    ...
};

The idea is that the template effectively turn out to be this when compiled:

int luaU_func(lua_State* L)
{
     luaU_push<int>(L, luaW_check<Foo>(L, 1)->baz(luaU_check<int>(L, 2), luaU_check<float>(L, 3)));
     return 1;
}

I’ve tried just using the ... expander, the problem for me is the integer index values to map to the proper arguments. I can’t think of a way to get them working right. Is such a thing even possible?

(there is a little bit of magic going on here already; I wrote some templated wrappers for things like lua_push and lua_check. All of those existing wrappers can be found here)

  • 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-09T21:10:17+00:00Added an answer on June 9, 2026 at 9:10 pm

    The trick is to exploit template argument deduction by partially specializing a class template that contains the wrapper function:

    // Lua API dummies ...
    
    struct lua_State {};
    
    template<class T> void luaU_push(lua_State*,T);
    template<class T> T* luaW_check(lua_State*,int);
    template<class T> T luaU_check(lua_State*,int);
    
    
    // metaprogramming for creating indices ...
    
    template<int...Ints>
    struct int_pack {};
    
    template<int Begin, int Count, int...Tail>
    struct make_int_range_type {
        typedef typename make_int_range_type<Begin,Count-1,Begin+Count-1,Tail...>::type type;
    };
    
    template<int Begin, int...Tail>
    struct make_int_range_type<Begin,0,Tail...> {
        typedef int_pack<Tail...> type;
    };
    
    template<int Begin, int Count>
    inline typename make_int_range_type<Begin,Count>::type
    make_int_range()
    { return typename make_int_range_type<Begin,Count>::type(); }
    
    
    // the actual wrapper ...
    
    template<class MemFunPtrType, MemFunPtrType PMF>
    struct lua_mem_func_wrapper;
    
    template<class Clazz, class ReturnType, class...Args, ReturnType(Clazz::*PMF)(Args...)>
    struct lua_mem_func_wrapper<ReturnType(Clazz::*)(Args...),PMF> {
        static int doit(lua_State* L) {
            return doit_impl(L,make_int_range<2,sizeof...(Args)>());
        }
    private:
        template<int...Indices>
        static int doit_impl(lua_State* L, int_pack<Indices...>) {
            luaU_push<ReturnType>(L,
                (luaW_check<Clazz>(L, 1)->*PMF)(
                    luaU_check<Args>(L, Indices)...
                )
            );
            return 1;
        }
    };
    
    #define GET_MEM_FUN_WRAPPER(...) &lua_mem_func_wrapper<decltype(__VA_ARGS__),__VA_ARGS__>::doit
    
    
    // testing ...
    
    struct foo {
        int baz(int, float);
    };
    
    void test() {
        auto* ptr = GET_MEM_FUN_WRAPPER(&foo::baz);
    }
    

    This code compiles under G++ 4.6.1 using the options -c –std=c++0x. To see whether it really does what you want, please test it …

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

Sidebar

Related Questions

In my current project, I am writing code generator. The interface would be Command
As part of my current project I've been tasked with collecting URL's for various
Current Project Setup I've been working on a web-based chat, similar to Facebook chat.
My current project requires extensive use of bit fields. I found a simple, functional
This current project I've been assigned uses the Version 3.1 levels of: Microsoft.Practices.EnterpriseLibrary.Common; Microsoft.Practices.EnterpriseLibrary.Data;
My current project is getting awfully large. I have dozens of activities, adapters, fragments,
The current project runs under cocos2d v2. I have a simple UITextField added to
Current project recently had a mess of changes to the DB schema. These were
My current project involves writing Perl code inside a Solaris VMWare appliance (hosted on
On my current project, we've been using Struts 1 for the last few years,

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.