tags:

views:

233

answers:

6

I'm not sure I know how to ask this.

say I have a function

void myFunc ( int8 foo, float bar, int whatever )
{
...
}

is there a quick way of referencing a particular argument by its position?

void myFunc ( float foo, float bar, float whatever )
{
   float f;
   f = ARG[1]; // f now equals bar
}

something to that effect?

Follow up:

Thank you for your answers folks. I guess I'm going about it wrong. I find it odd that c++ doesn't allow for this, as perl and some psuedo languages (I'm thinking in particular of AutoIt) do. So as for "why"? Just to use a simple loop to go through them. I recognize that there are a myriad of better ways to achieve this in normal circumstances, but I was trying my darndest to not modify anyone's code outside of my little world. In other words I don't have control over the calling code. It is shoving the inputs down my throat and I have to manage them as best as possible. So I can't just loop before calling my function. Anyway, it was clearly going to be a mess and there weren't that many variables so I just duplicated code. No biggy. Thanks for the comments and interesting suggestions.

+2  A: 

No there is not a standards compliant way to do this.

JaredPar
+4  A: 

Not in C or C++, no.

As moonshadow suggests: what actual problem are you trying to solve?

(If you want to add an explanation, please edit your question rather than leaving a comment on this answer - more people will see your edit that way.)

RichieHindle
+1  A: 

Try the <cstdarg> header.

Just remember that you will not get indexed access to the arguments, but you can write a loop and call va_arg till you reach the argument you want. Figuring out the argument's data type will be your next problem.

Vulcan Eager
+2  A: 

You can use C-style variable arguments:

#include <stdarg.h>

void myFunc (int8 foo, ...)
{
  va_list args;
  va_start(args, foo);

  float bar = va_arg(args, float);
  int whatever = va_arg(args, int);

  va_end(args);
}

but this means that:

  1. You need to know, in advance, the parameters that will be used to call the function.
  2. You do not get any benefits of static type checking.
In C++, there is almost no reason at all to do this. If you do it anyway, you lose all static checking of function parameters. Now what's the value in this?
sbi
+2  A: 

If it really seems like what you need is a parameter array of values of the same type instead of explicitly named parameters, then you can just pass an array as a parameter.

void myFunc ( float foo[3] )
{
    float bar;
    bar = foo[1];
}

That can be inefficient if your array is much longer, so a better solution would be using a const reference like this:

void myFunc ( const float & foo[3] )
{
    float bar;
    bar = foo[1];
}

Or like so many c++ questions on this site, the best solution is to use a std::vector

void myFunc ( const std::vector<float> & foo )
{
    float bar;
    bar = foo[1];
}
Alan
+8  A: 

May be boost::tuple is what you need?

#include <boost/tuple/tuple.hpp>

void myFunc ( const boost::tuple<int, float, double>& t )
{
   float f;
   f = boost::get<1>(t); // f now equals bar
}

int main()
{
    myFunc( boost::make_tuple( 1, 2.0f, 3.0 ) );
}

It gives you static type checking and you could get elements by its position. Tuples are part of future standard. It could be used as std::tr1::tuple already with some compilers.

If all arguments are the same type you could use std::vector:

#include <vector>

void myFunc ( const std::vector<float>& t )
{
   float f;
   f = t[1]; // f now equals bar
}

int main()
{
    std::vector<float> f_array;
    f_array.push_back( 1.0f );
    f_array.push_back( 2.0f );
    f_array.push_back( 3.0f );
    myFunc( f_array );
}
Kirill V. Lyadvinsky