Functions with a variable number of arguments

This is a discussion on Functions with a variable number of arguments within the C++ Programming forums, part of the General Programming Boards category; //-------------------------------------------------------------------------------- template <class t_cElement, unsigned int t_uiDimensions> class g_cVector { protected: t_cElement m_eElements[t_uiDimensions]; public: explicit g_cVector(...); virtual ~g_cVector(); // And ...

  1. #1
    Unregistered
    Guest

    Functions with a variable number of arguments

    //--------------------------------------------------------------------------------

    template <class t_cElement, unsigned int t_uiDimensions>
    class g_cVector
    {
    protected:
    t_cElement m_eElements[t_uiDimensions];
    public:
    explicit g_cVector(...);
    virtual ~g_cVector();

    // And some more functions...
    };

    //--------------------------------------------------------------------------------

    template <class t_cElement, unsigned int t_uiDimensions>
    inline g_cVector::g_cVector(...)
    {
    /*
    I've tried to use the stdarg macros to get and process the
    arguments, but it seems extremely slow and the code it
    produces is extremely ugly. I've been thinking about using
    template metaprogramming to do what I want but I'm very
    uncertain of what to do with this.

    m_eElements[0] = ?;
    m_eElements[1] = ?;
    m_eElements[2] = ?;
    ...

    until all the data is processed (don't forget we've got the t_uiDimensions template thing)
    ....
    ehh
    can something like this be achieved in some simple way?
    */
    }

    //--------------------------------------------------------------------------------

    template <class t_cElement, unsigned int t_uiDimensions>
    inline g_cVector::~g_cVector()
    {
    }

    //--------------------------------------------------------------------------------

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    I think the va_lists are pretty clean but I guess it depends on what you need to do. I've never done a speed test on them but this is the basic way to use them (for those who haven't ever). What are you trying to accomplish?

    stdarg library - va_list

    simple usage:

    Code:
    void VariableArgFunction( int NumArgs, ... ) {
    
      /* a pointer to the list of arguments */
      va_list ArgList;
    
      /* loop counter variable */
      int i;
    
      /* initialize the list */
      va_start( ArgList, NumArgs );
    
      /* process the variables in the list */
      for( i = 0; i < NumArgs; i++ ) {
        printf( "Argument %d:%d\n", i, va_arg( ArgList, int ) );
      }
    
      /* when we're done with the list */
      va_end( ArgList );
    }
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  3. #3
    Unregistered
    Guest
    I'm trying to do almost exactly what you demonstrated, but instead of printing everything out placing it in an array and instead of using a for loop using something that can be doon i the preprocessor. The thing is that I don't know how to do it in the preprocessor, but I've heard it's possible to use "template metaprogramming" to achieve this effect...that is also the reason why I'm using a template argument with the amount of dimensions the vector is to have.
    I simply want to repeat something in the preprocessor to effectively read the arguments of the function (which ofcourse are of the type t_cElement )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. macros with unspecified number of arguments
    By TriKri in forum C Programming
    Replies: 17
    Last Post: 08-01-2006, 07:40 PM
  2. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  3. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 01:26 PM
  5. Overloading functions with variable arguments
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-08-2001, 08:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21