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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:12:57+00:00 2026-05-28T03:12:57+00:00

I’m working on something that allows me to make it easier to call functions

  • 0

I’m working on something that allows me to make it easier to call functions in a scripting language (Lua) and one idea was to use variadic arguments. There are two ways to do that:

Call( int count, ... )

and

Call( const char* args, ... )

like printf. I thought the first one would be a little easier to maintain, so I tried doing it by making a wrapper to manage types of arguments. It looks like this:

class ArgWrapper
{
public:
    ArgWrapper();

    ArgWrapper( const int& v ) { val.i = v; type = INT; }
    ArgWrapper( const bool& v ) { val.b = v; type = BOOL; }
    ArgWrapper( const float& v ) { val.f = v; type = FLOAT; }
    ArgWrapper( const double& v ) { val.d = v; type = DOUBLE; }

    operator int() { return val.i; }
    operator bool() { return val.b; }
    operator float() { return val.f; }
    operator double() { return val.d; }

    enum {
        INT,
        BOOL,
        FLOAT,
        DOUBLE
    } type;

    union
    {
        int i;
        bool b;
        float f;
        double d;
    } val;
};

This works fine and dandy, but when I actually try to make this work in a varadic arguments construction, it turns out that the values aren’t actually casted.

void printArgs( int c, ArgWrapper... )
{
    va_list ap;
    va_start( ap, c );

    for ( int i = 0; i < c; i++ )
    {
        ArgWrapper arg = va_arg( ap, ArgWrapper );
        if ( arg.type == arg.INT ) std::cout << "integer - " << (int)arg << std::endl;
        if ( arg.type == arg.BOOL ) std::cout << "boolean - " << std::boolalpha << (bool)arg << std::endl;
        if ( arg.type == arg.FLOAT ) std::cout << "float - " << (float)arg << std::endl;
        if ( arg.type == arg.DOUBLE ) std::cout << "double - " << (double)arg << std::endl;
    }

    va_end( ap );
}

...

printArgs( 4, 1337.0f, 18, 37.0, true );

The above results in garbage, while this works perfectly fine:

printArgs( 4, (ArgWrapper)1337.0f, (ArgWrapper)18, (ArgWrapper)37.0, (ArgWrapper)true );

How do I specify that each variadic argument should have the same common type?

  • 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-28T03:12:58+00:00Added an answer on May 28, 2026 at 3:12 am

    When you use C++03, you can’t, since variadic arguments using the c-style ellipsis ... do not carry type information in any way. This is why stuff like printf() uses format specifiers, since it needs a way to know what to cast stuff to.

    When you can use C++11, you should use variadic templates right away which would make any kind of helper like your ArgWrapper unnecessary.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
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 am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.