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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:24:12+00:00 2026-06-01T00:24:12+00:00

I have the following templated function… template< class T > T *create_object( lua_State *L

  • 0

I have the following templated function…

template< class T > T *create_object( lua_State *L )    
{
    // Get a raw block of memory, managed by Lua.
    void *mem = lua_newuserdata( L, sizeof( T ) );
    // Construct the object in the allocated memory.
    T *object = new (mem) T;
    // Do other stuff here...
    return object;
}

… that allocates and sets up a C++ object for use in the Lua scripting language. I’d like to extend this function so I can pass in arguments for the object’s constructor. It might look something like this:

template< class T > T *create_object( lua_State *L, ??? ctor_args )    
{
    void *mem = lua_newuserdata( L, sizeof( T ) );
    T *object = new (mem) T( ctor_args ); // Call correct constructor as determined by args.
    // ...
    return object;
}

… and work something like this:

class widget
{
    public:
        // Multiple constructors
        widget(); // #1
        widget( const widget &w ); // #2
        widget( int width, int height, float x, float y ); //#3
};

class font
{
    public:
        font( std::vector<uint8_t> );
}

// Other classes with multiple constructors.

// Example usage: (L = lua_State pointer)
create_object<widget>( L ); // Pass no arguments - use constructor #1
create_object<widget>( L, existing_widget ); // Pass one argument- use constructor #2
create_object<widget>( L, 128, 64, 100.0f, 100.0f ); // Pass 4 arguments - use construct #3
create_object<font>( L, buffer ); // Just to show it needs to work with many object types...
... and so on ...

Avoiding a variadic template that ends up like this:

create_object<widget, int, int, float, float >( L, 256, 512, 120.0f, 0.0f );

Would be nice.

Is this possible in c++11?

Update: I’m currently using gcc 4.6 with -pedantic turned on. Non compiler-specific solutions would be preferred.

  • 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-01T00:24:13+00:00Added an answer on June 1, 2026 at 12:24 am

    Like this, provided you have proper support for variadic templates:

    template< class T, typename... Args > 
    T *create_object( lua_State *L, Args&&... args)    
    {
        void *mem = lua_newuserdata( L, sizeof( T ) );
        T *object = new (mem) T(std::forward<Args>(args)...);
        // ...
        return object;
    }
    

    This will correctly forward references, even your constructor takes some of its arguments by (const or not) reference and other by value.

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

Sidebar

Related Questions

Say I have the following template function: template <class T> void apply(const vector<complex<T> >&
A template function I have written has the following signature: template<class IteratorT> auto average(IteratorT&
I have the following templated class structure struct TraitA{}; struct TraitB{}; template<typename trait> struct
suggest i have a template function like following: template<class T> void doSomething() { T
Inside my template function I have the following code: TypeName myFunction() { TypeName result;
I have the following pattern: template <int a, int b> class MyClass { public:
I have the following code: template <class T> struct pointer { operator pointer<const T>()
I have the following problem using template instantiation [*]. file foo.h class Foo {
I have the following class template: template<class T, unsigned N> class MyClass; where T
I have the following template class: template<class T> class C { typedef C_traits<T> traits;

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.