I want to create a template function that casts lambdas to function pointers of different calling conventions.
template<typename RetType, typename... Args>
auto fastcall_cast(LambdaType lambda) -> RetType(__fastcall *)(Args...)
{
return static_cast<RetType(__fastcall *)(Args...)>(lambda);
}
int main() {
auto pFunc = fastcall_cast([](int a, int b) -> int {
return a + b;
});
// this is what I want to replicate without the boilerplate:
auto pFunc2 = static_cast<int(__fastcall *)(int, int)>([](int a, int b) -> int {
return a + b;
});
};
I have been relying on this macro previously but I find it quite ugly as it requires an extra closing parenthesis in the end:
#define INLINE_HOOK(retnType, callingConv, ...) static_cast<retnType(callingConv*)(__VA_ARGS__)>([](__VA_ARGS__) [[msvc::forceinline]] -> retnType
WriteRelCall(0x4909BC, INLINE_HOOK(UInt32, __fastcall, BSAnimGroupSequence* anim)
{
if (anim->textKeys->FindFirstByName("allowClampInLocomotion"))
return NiControllerSequence::kCycle_Loop;
return anim->cycleType;
}));
It is not possible to specify a calling convention directly inside a lambda in VC++ as it'll simply ignore it with a warning stating warning C4229: anachronism used: modifiers on data are ignored
. Apparently you need to cast it.
Ideally the template function does not require any explicit template arguments and is able to be deduced from the lambda itself.
Is this possible in Visual C++?
EDIT: For some more context, this is for writing hooks into a game during runtime. It needs specific calling conventions so that the hooks are compatible with the game's native functions which are usually __thiscall
. WriteRelCall takes the pointer value of the function, and replaces the x86 assembly in the game with the new function.
Wit MSVC x86 target, first we need an arg forwarder different from std::forward
:
template<typename targ>
[[msvc::intrinsic]]
constexpr decltype(auto) fast_fwd(std::type_identity_t<targ>& arg){
/*if constexpr(std::is_reference_v<targ>
or not std::is_trivial_v<targ>)
return static_cast<targ&&>(varg);
else*/
return static_cast<targ>(varg);
};
This forwarder captures trivial arguments by value. On second thought, it should be a simple static cast. But the abstraction allows for later tweaks.
Next a detail only dirty template function:
template<auto lambda, typename R, typename ...A>
R __fastcall as_fast_impl(A ...v){
return lambda(fast_fwd<A>(v)...);
};
Now a declare template variable with usless implementation:
template<auto lambda, typename fptr = decltype(+lambda)>
constexpr auto as_fast = nullptr;
Next specialize the real use case:
template<auto lambda, typename R, typename ...A>
auto constexpr as_fast<lambda, R(*)(A...)> = as_fast_impl<lambda, R, A...>;
The cast is ready. We can make fastcall pointers out of empty capture lambdas
auto fast_call_ptr = as_fast<
[](int,int)->int{
return 0;
}>;
This is a C++20/C++23 solution. Aside from typos in my snippet, due to a now-resolved defect in std wording, some combinations compiler version and std version setting may reject lambda as NTTP. Otherwise, the code tries to optimize away copy/move of argument; but in general, if arguments are none trivial and none reference, copy/moves will be inferred into the fastcall wrapper. Moreover the lambda needs to have single none-generic (no [](auto...)
), none-template(no []<A...>
) function call operator.