tags:

views:

69

answers:

2

Can I suppose that, from the call stack point view, it's the same to call a function like function1

int function1(T1 t1, T2 t2);

than to another like function2?

struct parameters_t
{ 
    Wide<T1>::type t1;
    Wide<T2>::type t2;
}

int function2(parameters_t p);

Where, Wide template wide T to the processor word length. For example, for 32-bit processors:

template<typename T, bool b = sizeof(T) >=4 >
struct Wide
{
    typedef T type;
};

template<typename T>
struct Wide<T,false>
{
    typedef unsigned int type;
};

I need to do something like this:

typedef int (*function_t)(parameters_t);

function_t function = (function_t) &function1;

parameters_t params;
// initialize params
function(params);

Thanks!

+1  A: 

What you're asking is actually two different questions. I don't think there's any guarantee that the two methods will look the same on the call stack, but for your purposes of needing to pass parameters to a function called by a pointer, it works.

You should probably consider passing it via a reference or pointer though. Passing a large structure around over and over will be inefficient.

San Jacinto
+2  A: 

Question 1. No the two function calls aren't necessarily the same -- calling conventions that push parameters right to left and left to right are both in wide use.

It sounds like you want to create a function that takes a variable number of a variable type of parameters. To do that, I'd have it take something like an std::vector<boost:any> as its parameter.

Jerry Coffin
You are of course correct and are saying the same thing I am, but I'd go one step further if you're going to use this method and put that vector into an args class or struct, and then template that so that later on you can add more arguments of a different type without any change in the API.
San Jacinto
@San Jacint:Oh yeah, there are definitely quite a few variations on the theme. The bottom line is to figure out what you really need, and then do it (directly). If you lie to the compiler, it will get its revenge, and trying to pass individual parameters to something defined to take a struct (or vice versa) falls directly into that category...
Jerry Coffin
Ok. It works because I use gcc and it push parameters right to left, but it's true that there are variations between different platforms over register usage.I don't want the compiler to take revenge on me later :)Thanks you both!
cu3dots