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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:59:24+00:00 2026-05-19T16:59:24+00:00

I want users of my C++ application to be able to provide anonymous functions

  • 0

I want users of my C++ application to be able to provide anonymous functions to perform small chunks of work.

Small fragments like this would be ideal.

function(arg) return arg*5 end

Now I’d like to be able to write something as simple as this for my C code,

// Push the function onto the lua stack
lua_xxx(L, "function(arg) return arg*5 end" )
// Store it away for later
int reg_index = luaL_ref(L, LUA_REGISTRY_INDEX);

However I dont think lua_loadstring will do “the right thing”.

Am I left with what feels to me like a horrible hack?

void push_lua_function_from_string( lua_State * L, std::string code )
{
   // Wrap our string so that we can get something useful for luaL_loadstring
   std::string wrapped_code = "return "+code;
   luaL_loadstring(L, wrapped_code.c_str());
   lua_pcall( L, 0, 1, 0 );
}

push_lua_function_from_string(L, "function(arg) return arg*5 end" );
int reg_index = luaL_ref(L, LUA_REGISTRY_INDEX);

Is there a better solution?

  • 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-19T16:59:25+00:00Added an answer on May 19, 2026 at 4:59 pm

    If you need access to parameters, the way you have written is correct. lua_loadstring returns a function that represents the chunk/code you are compiling. If you want to actually get a function back from the code, you have to return it. I also do this (in Lua) for little “expression evaluators”, and I don’t consider it a “horrible hack” 🙂

    If you only need some callbacks, without any parameters, you can directly write the code and use the function returned by lua_tostring. You can even pass parameters to this chunk, it will be accessible as the ... expression. Then you can get the parameters as:

    local arg1, arg2 = ...
    -- rest of code
    

    You decide what is better for you – “ugly code” inside your library codebase, or “ugly code” in your Lua functions.

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

Sidebar

Related Questions

I would like to enable users of my we application to be able to
I want to create a small helpdesk ticket control system at work, that would
I want users of my c# application to be able to download a file
I have a WinForms application in which I want to be able to provide
I want the user to be able to see my application in the menu
in our AIR application, I want to user to be able to download a
I want users to receive 'points' for completing various tasks in my application -
I want to allow users of my application to add sub-users and set privileges
I want to build a web application where users can register and have a
In my web application we have many users.I want to set permission for each

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.