Struct with the function parameters
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, 开发者_如何学PythonWide 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!
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.
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.
精彩评论