Results 1 to 9 of 9

Thread: [C++] Function with undefined number of parametes?

  1. #1
    Join Date
    Mar 2006
    Beans
    837

    [C++] Function with undefined number of parameters?

    While experimenting with gstreamer(written in C) I saw many functions taking many parameters, without having a fixed number of them, and the last is a NULL parameter.
    1.How do I implement this kind of function in C++?
    2.How do I know how many parameters where passed?
    example pseudo code:
    Code:
    my_custom_function(int parm1, int parm2, int parm3 ... int parmN, NULL)
    Last edited by SledgeHammer_999; September 8th, 2008 at 02:45 PM.

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [C++] Function with undefined number of parametes?

    Quote Originally Posted by SledgeHammer_999 View Post
    While experimenting with gstreamer(written in C) I saw many functions taking many parameters, without having a fixed number of them, and the last is a NULL parameter.
    1.How do I implement this kind of function in C++?
    2.How do I know how many parameters where passed?
    example pseudo code:
    Code:
    my_custom_function(int parm1, int parm2, int parm3 ... int parmN, NULL)
    http://www.codersource.net/c++_varia...functions.html

    http://www.cppreference.com/stdother/va_arg.html

    It should explain it.

  3. #3
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: [C++] Function with undefined number of parametes?

    For you to know, in C (examples taken from a real piece of code by me I hope to release soon):

    0. #include <stdarg.h> It's a Standard Library's header, so you don't need to install anything special.

    1. You declare the function as:
    Code:
    char list_set(list_t *list, void *data, ...)
    The ellipsis (...) tells the compiler the function will have an undefined amount of parameters. Note that functions have to have at least one required argument.

    2. Look at this code (which is from a real program I'm developing, to show what's the usual way to use this). Don't try to understand what it does, but rather read the comments.
    Code:
    char list_set(list_t *list, void *data, ...)
    {
        char exit_code = 0;
        va_list ap; /* Variable parameters list declared */
        va_start(ap, data); /* Initializing 'ap' to take parameters up from 
                               argument 'data'. */
       
        /* Parse arguments */
        void *i = NULL; /* Now, we have to iterate through 'ap', but the counter has
                           to be (void *) because that's the type I need. But it 
                           should be of the data type you expect from arguments. */
        size_t j;
        for(i = data, j = 0; i != NULL; i = va_arg(ap, void *), j++)
            /* va_arg() returns the next argument from 'ap' that corresponds to the 
               data type I request it to be (void *) in this case. As you see, if 
               the parameter is NULL, the for-loop stops. */
        {
            node_t *curr = LIST_NODE(list, j);
    		
            if(curr == NULL)
            {
                exit_code = list_append_data(list, j - 1, i);
                if(exit_code == 1)
                    break;
            }
            else
                node_set_data(curr, i); /* Here I used the parameter I've taken. */
        }
        
        va_end(ap); /* You ALWAYS have to "close" the arguments list. */
    
        return exit_code;    
    }
    3. So, basically, you create a va_arg variable, initialize it with va_start() and then, you request the next argument with va_arg(). And when you're done, you close it with va_end()

  4. #4
    Join Date
    Jul 2008
    Location
    Dublin, Ireland
    Beans
    633
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [C++] Function with undefined number of parameters?

    Quote Originally Posted by SledgeHammer_999 View Post
    While experimenting with gstreamer(written in C) I saw many functions taking many parameters, without having a fixed number of them, and the last is a NULL parameter.
    1.How do I implement this kind of function in C++?
    2.How do I know how many parameters where passed?
    example pseudo code:
    Code:
    my_custom_function(int parm1, int parm2, int parm3 ... int parmN, NULL)
    In general I would advice against variable argument methods (with varargs) as it is harder to use generic code on that kind of methods (some libraries don't even support it). In some cases (as gstreamer) where they must provide for a completely general unknown at library-compile time interface, it may be the only option, but in general if you can make it with just default values for your methods, that plays better with the rest of the language.

    Code:
    void f( int a = 0, double b = 0.0, std::string value = "" ); 
    
    f(); // calls f(0,0.0,"")
    f(1); // calls f(1,0.0,"")
    f(2,3.0); // calls f(2,3.0,"")
    f(4,5.0,"Hi");
    Limitations are: you can only omit parameters from the right (you cannot provide just the string value) and you must have a predefined set of maximum parameters (and order).

    Not as flexible as varargs, but easier to use with generic code (boost::bind, boost::lambda, boost::signal, boost::function...)

    David

  5. #5
    Join Date
    Mar 2006
    Beans
    837

    Re: [C++] Function with undefined number of parameters?

    I think I understand now Ehm just one question: from the examples I read: When I call the function I don't need to pass NULL as the final parameter, right?

    @dribeas I don't like to use variable argument methods but in case of my program I need to pass to a pipeline a variety of elements(plugins/decoders/filters). I can't know from the beginning how many elements I'll need to decode a certain media type.

  6. #6
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: [C++] Function with undefined number of parameters?

    Quote Originally Posted by SledgeHammer_999 View Post
    I think I understand now Ehm just one question: from the examples I read: When I call the function I don't need to pass NULL as the final parameter, right?
    At least in my C example, yes. Because the for-loop that processes arguments is told to stop when the argument is NULL; surely gstreamer does the same (and GTK+ also uses the same idiom).

    If you don't pass NULL, the result is undefined... usually it works (for me), but you aren't safe from possible weirdnesses because of that, so better don't do that and follow the API!

  7. #7
    Join Date
    Mar 2006
    Beans
    837

    Re: [C++] Function with undefined number of parameters?

    yeah I figured that I 'need' to pass NULL so I'll know where to stop(my code). But I have another question. Which is the correct header for C++ stdargs.h or cstdarg?

  8. #8
    Join Date
    Jul 2008
    Location
    Dublin, Ireland
    Beans
    633
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [C++] Function with undefined number of parameters?

    Quote Originally Posted by SledgeHammer_999 View Post
    yeah I figured that I 'need' to pass NULL so I'll know where to stop(my code). But I have another question. Which is the correct header for C++ stdargs.h or cstdarg?
    Ending your parameter sequence with NULL (in C, 0 in C++) is optional, as long as you can figure the parameter list size. printf and all its variants don't require the size of the argument list, it can be inferred from the first char* parameter.

    In general, for all C standard header xxx.h there is a related cxxx C++ header. In most cases it is just a wrapper that places the functions inside the std namespace. In some cases it also undefines macros and implements them as inline functions. You should go for the cxxx version whenever available (if you code C++).

  9. #9
    Join Date
    Aug 2008
    Location
    OHIO
    Beans
    281
    Distro
    Xubuntu 9.04 Jaunty Jackalope

    Re: [C++] Function with undefined number of parameters?

    maybe it is just me but I have always hated this construct.

    You could just as easily make the parameter a single argument of type vector and get the same functionality with a cleaner construct.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •