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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:25:35+00:00 2026-05-30T12:25:35+00:00

I have a generic function for pushing stuff on to the lua stack called

  • 0

I have a generic function for pushing stuff on to the lua stack called luaU_push, which must be specialized for any type that wants to use it. For example:

template <>
inline void luaU_push<>(lua_State* L, const Vector2i& val)
{
    lua_newtable(L);
    luaU_setfield<int>(L, -1, "x", val.x);
    luaU_setfield<int>(L, -1, "y", val.y);
}

It turns out that Vector2i is actually a typedef. The real type is Vector2<int>. In a few other places I use Vector2fs, which are just a typedef for Vector2<float>.

I’d like to be able to have a luaU_push for Vector2fs. I could make a duplicate function for Vector2f, but I’d prefer to make this one generic so I can use it on any kind of Vector2<T>, but I can’t figure out the syntax to do that. I thought that I could do something like this, but this does not appear to work:

template <>
template <typename T>
inline void luaU_push<>(lua_State* L, const sf::Vector2<T>& val)
{
    lua_newtable(L);
    luaU_setfield<T>(L, -1, "x", val.x);
    luaU_setfield<T>(L, -1, "y", val.y);
}

Is there a way to get this working the way I want?

Edit:

Follow up question: I had intended to use the answer to this question to fix a set of functions, including a few that only differ by return type, but I don’t think the answer given is sufficient for this. For example, I have this function (which is basically the opposite of the function above)

template <>
inline sf::Vector2i luaU_to<>(lua_State* L, int index)
{
    return sf::Vector2i(
        luaU_getfield<int>(L, index, "x"),
        luaU_getfield<int>(L, index, "y"));
}

I don’t believe that there’s a way to use overloading to make this work in a generic way, and I can’t use partial specialization. Is there any way to make it work in this case?

  • 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-30T12:25:37+00:00Added an answer on May 30, 2026 at 12:25 pm

    Take this answer as a COMMENT.

    template < typename T >
    inline void luaU_push(lua_State* L, const sf::Vector2<T>& val)
    {
        lua_newtable(L);
        luaU_setfield<T>(L, -1, "x", val.x);
        luaU_setfield<T>(L, -1, "y", val.y);
    }
    

    This should work. And in case of templated functions, a "base template" function will always have higher priority than a fully specialized one.

    EDIT ::

    You have overloaded the function “luaU_to” based upon the return type ! Which is not allowed and possible (unless you use some un-understandable/untidy nasty trick)

    What you can do is, create a specialized version for each return data type and dont let compiler do the argument deduction for i.e mention the data type explicitly in the template call.

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

Sidebar

Related Questions

I have a web service which has a generic function that returns a dataset
I have a function that accepts a generic parameter T that is of type
I have a small C# function that takes any generic SQLDataReader and converts it
I have a javascript function that calls a generic function to make an ajax
I have: void add_all_msgs(std::deque<Message>::iterator &iter); How can I make that function generic, so it
I have generic list which must be a preserved order so I can retrieve
I have generic type that looks like: public class GenericClass<T, U> where T :
I have a generic function that is constrained to struct. My inputs are boxed
I want to have a generic PHP function that builds a mysql query based
I have a generic function that returns URLs. (It's a plugin function that returns

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.