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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:47:38+00:00 2026-06-06T12:47:38+00:00

I’ve got an API (a specific GUI library) that relies on std::shared_ptr a lot,

  • 0

I’ve got an API (a specific GUI library) that relies on std::shared_ptr a lot, i.e. they are often used as function parameters and stored within other objects. For example, container widgets, such as splitters and boxes will store their child widgets in shared_ptrs. Now I would like to map this API to Lua via luabind. In an ideal world, Luabind would create new objects in shared_ptrs and allow me to pass those directly to functions taking shared_ptr parameters. This seems to work for single classes, e.g.:

luabind::class_<Button, std::shared_ptr<Button>>("Button")

While I declare it like that, I can expose and use functions like void foo(std::shared_ptr<Button> const&).

Now the luabind manual mentions that in order to use a hierarchy of classes, I’d have to use the same shared_ptr template-instance for all classes in the hierarchy, e.g.

luabind::class_<BaseWidget, std::shared_ptr<BaseWidget>>("BaseWidget"),
luabind::class_<Button, BaseWidget, std::shared_ptr<BaseWidget>>("Button")

Now I can no longer call foo – it will fail to find the function from Lua. Can I somehow get luabind to still support passing buttons in shared_ptrs? Also, I would like to know why luabind mandates that you use the same smart pointer for all classes in the hierarchy instead of them just being convertible to base class pointers.

  • 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-06T12:47:40+00:00Added an answer on June 6, 2026 at 12:47 pm

    I think for that to work you have to bind your derived class like this:

    luabind::class_<Button, BaseWidget, std::shared_ptr<Button>> ("Button")
    

    For example:

    class BaseWidget
    {
    public:
        static void Bind2Lua(lua_State* l)
        {
            luabind::module(l)
            [
                luabind::class_<BaseWidget, std::shared_ptr<BaseWidget>> ("BaseWidget")
                .def(luabind::constructor<>())
            ];
        }
    
        virtual ~BaseWidget()
        {
    
        }
    };
    
    class Button : public BaseWidget
    {
    public:
        static void Bind2Lua(lua_State* l)
        {
            luabind::module(l)
            [
                luabind::class_<Button, BaseWidget, std::shared_ptr<Button>> ("Button")
                .def(luabind::constructor<>())
                .def("Click", &Button::Click)
            ];
        }
    
        void Click()
        {
            std::cout << "Button::Click" << std::endl;
        }
    };
    

    Now you can use it with shared_ptr:

    class Action
    {
    public:
        void DoClick(const std::shared_ptr<Button>& b)
        {
            // perform click action
            b->Click();
        }
    
        static void Bind2Lua(lua_State* l)
        {
            luabind::module(l)
            [
                luabind::class_<Action> ("Action")
                .def(luabind::constructor<>())
                .def("DoClick", &Action::DoClick)
            ];
        }
    };
    

    In lua:

    b = Button()
    
    a = Action()
    
    a:DoClick(b)
    

    The reason is that luabind uses type-id system with integers (more precisely std::size_t as defined in inheritance.hpp).
    You can obtain the type-id of any registered type with the function:

    luabind::detail::static_class_id<T>(nullptr);
    

    Where T is the registered class.
    In my demo program they are:

    • BaseWidget = 3
    • std::shared_ptr< BaseWidget > = 6
    • Button = 4
    • std::shared_ptr< Button > = 7
    • Action = 5

    So when you call DoClick from lua, it will call the get member of the template class pointer_holder in instance_holder.hpp:

    std::pair<void*, int> get(class_id target) const
    {
        if (target == registered_class<P>::id)
            return std::pair<void*, int>(&this->p, 0);
    
        void* naked_ptr = const_cast<void*>(static_cast<void const*>(
            weak ? weak : get_pointer(p)));
    
        if (!naked_ptr)
            return std::pair<void*, int>((void*)0, 0);
    
        return get_class()->casts().cast(
            naked_ptr
          , static_class_id(false ? get_pointer(p) : 0)
          , target
          , dynamic_id
          , dynamic_ptr
        );
    }
    

    As you can see, if the target class is not the same as the one registered, it will try to do a cast.
    This is where things get interesting.
    If you declared the Button class as

    luabind::class_<Button, BaseWidget,std::shared_ptr<BaseWidget>>("Button")
    

    then the instance will be held as a shared_ptr to BaseWidget, thus the cast function will try to cast from BaseWidget (3) to std::shared_ptr< Button > (7) and that fails. It could work if luabind supported base-to-derived conversion, which it doesn’t seem to.

    If however you declared the Button class as

    luabind::class_<Button, BaseWidget, std::shared_ptr<Button>> ("Button")
    

    then the instance will be held as as a shared_ptr to Button and then the target id will match the registered type. The get function will branch on the first return, never to bother with the cast.

    You can also find the self contained program I used here at pastebin.

    And here is a list of interesting break points you can set to see what’s happening (luabind version 900):

    • line 94 in instance_holder.hpp (first line of pointer_holder::get)
    • line 143 in instance.cpp (first line of cast_graph::impl::cast)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
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 used javascript for loading a picture on my website depending on which small
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
i got an object with contents of html markup in it, for example: string

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.