last day (15 days later) » 

12:56 AM
2
Q: Store a parameter pack for a function whose return type is known later

nowiHow can one store a parameter pack that will be passed as parameters to a function (later, not immediately, so it needs to be in some member/var) that will be called later? e.g. class foo { void* fn; magic_type x; public: template<typename... t> set_parameters(t... params){ x = para...

 
I deleted the question as I got several comments asking to expand on the question and the 2 answers received did not even remotely answer the question. Hopefully this time it is clear.
 
"the function pointer is known at object creation and will not be passed like this" then maybe you should update your question to pass the function pointer via the constructor. This detail might affect the sort of answers you get.
I think you might be able to do this with std::bind
 
@Kerndog73 fixed.
@Kerndog73 interested note on std::bind. Please post an answer if you have an idea of what you are going for
@NicolBolas a pointer to a function that will be casted to a function via reinterpret_cast.
I guess it is implementation-specific. This is MSVC and it is using their WinAPI for grabbing function table pointers. They return void*
 
I don't think this is possible using the current interface. I think you'll have to make the function type a template parameter of foo. If you do that then you can use a tuple to store the arguments and there's no undefined behaviour.
 
@Kerndog73 When the object foo is created, nothing is known except the function pointer.
 
12:56 AM
Are you able to create foo later when you have more information?
 
@Kerndog73 unfortunately I cannot as the documentation for those WinAPI functions do not allow it safely. That said, the signature is known with 100% certainty so please do not worry about that :)
 
You could pass around a void *. Then when you're ready to set the parameters, you could do this: foo bar{ptr, 2.f, 2.f};. With class template argument deduction, this is pretty clean. foo could store a tuple of arguments. If you know the return type while you're setting the parameters then you could set that as well. Then the whole thing becomes trivial.
 
In the order listed in the example. The first thing known is the pointer to the function. The next thing known is the parameters of the function. The last thing known is the return type. Obviously, I cannot call the function with a partial signature so the example sets it up as actually calling the function in call. I suppose I can do some inline assembly to force the call or something and store the return until call is called and just cast the returned data.
@Kerndog73 the actual implementation is much cleaner that this. This is just supposed to be a minimal and simple example to get the question started. Thank you for the input, though.
 
Inline assembly isn't going to help you there. You need to know what you're returning in order to return it.
 
@NicolBolas I've updated the tags.
@Kerndog73 I figured I could store the return as a raw byte array of dynamic size then cast it later.
@KaenbyouRin thankfully WinAPI guarantees the safety
@eike interesting idea; it might work. I'd love to see your implementation of it as an answer
 
12:56 AM
If you get the return type wrong then who knows what will happen
 
@NicolBolas This is not production code, this is for malware analysis. Thankfully, WinAPI guarantees the signature will not fail
@Kerndog73 probably not important since it is guaranteed by WinAPI
@NicolBolas The parameters I'm passing are parameters I also receive from WinAPI. Please forget about this and focus on answering the actual question :)
 
I have a solution that might do what you want but it has a reinterpret_cast right in the middle. Perhaps you could tell us more about your problem and we can provide a cleaner solution.
 
@Kerndog73 I have no problem with reinterpret_cast; I'm not sure what you mean by "right in the middle". The problem is I'm trying to forward a function call from one winapi return to another winapi call. This is the cleanest way so my code does not become a living hell to maintain. This way I can focus on just sigantures and calls rather than an implementation for each forward.
 
Is it a problem for foo to be a template? If it is then I can implement what eike mentioned.
Actually, I'm not sure if that's possible.
 
foo cannot be templated. Only set_parameters and call
I may have a solution...
 
12:56 AM
eike overlooked the return type not being known until the call. What they described is almost possible.
 
posted a possible answer
 
1:11 AM
@nowi I was in the middle of typing up an answer for you (or more specifically detailing exactly why no answer is possible). But there's no real chance for that now, I guess.
Basically, unless you know for a fact that all of the parameters to these functions are values rather than references, you cannot do what you're attempting.
 
 
7 hours later…
7:54 AM
@NicolBolas The question was deleted for only a few minutes by a moderator until I made an edit. Feel free to post your answer.
 

last day (15 days later) »