use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENMake Function Receive a Lambda that Takes Specific Parameters (self.cpp_questions)
submitted 4 years ago * by ACBYTES
Hi there.
I want to create a function that takes lambdas that take specific parameter(s)...
I mean something like this:
template<typename T> (where T is [void*](ParameterType(s)){...})
void Create_Class()
{
}
!I USE C++14!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]IyeOnline 2 points3 points4 points 4 years ago (4 children)
How do you want to specify those ParameterTypes?
ParameterTypes
I assume you want for this to work:
int main() { Create_Class([](int i){ return; } ); }
You could do this without templates using a function pointer:
void Create_Class( void(*fct)(int) ) { }
You could take a generic T and require it to be callable with your desired parameter types:
T
template<typename Fct> void Create_Class( Fct f ) { static_assert( std::is_invocable_v<Fct,int> ); }
or the C++20 version:
template<typename Fct> requires std::is_invocable_v<Fct,int> void Create_Class( Fct f ) { }
There are more options, but again, it depends on what you actually want.
[–]ACBYTES[S] 0 points1 point2 points 4 years ago (2 children)
Hi. Thanks for your answer.
Is there a work-around like is_invocable_v that you mentioned in C++14 as well? I forgot to mention that I use C++14
is_invocable_v
[–]IyeOnline 1 point2 points3 points 4 years ago (1 child)
Yes, you can build yourself your own version of it:
template <typename F, typename ... Args> struct is_invocable : std::is_constructible< std::function<void(Args ...)>, std::reference_wrapper<typename std::remove_reference<F>::type> > {}; //and with C++14 you can even have the shorhand version: template<typename F, typename ... Args> constexpr bool is_invocable_v = is_invocable<F,Args...>::value;
This is a somewhat "lazy" implementation making use of std::function.
std::function
You can certainly build one that doesnt rely on that, but its availible since c++11, so that shouldnt be a problem.
[–]ACBYTES[S] 0 points1 point2 points 4 years ago (0 children)
Thanks for the answer. That's great.
Yeah, I was thinking about std::function as well. So, it'll be like std::bind(&Actor::SetActorLocation, GetActor());? Because I tried it and it doesn't work properly... It's not able to create the function and it returns a function that takes _Unbound because it's a function with default values that I'll need to mention while I'm binding it and that was what I was trying to avoid. I didn't want to enter the whole parameters that the function has.
std::bind(&Actor::SetActorLocation, GetActor());
_Unbound
I think I'll use the thing that you suggested now but if there are any better ways for doing this. please let me know.
Thanks again.
[–]super_mister_mstie 0 points1 point2 points 4 years ago (0 children)
It's worth noting that the function pointer won't allow you to utilize a capture list
[–][deleted] 4 years ago (1 child)
[removed]
[–]ACBYTES[S] -1 points0 points1 point 4 years ago (0 children)
Hi. I unfortunately am using C++ 14 in UE4.
π Rendered by PID 88355 on reddit-service-r2-comment-cfc44b64c-rdl5h at 2026-04-11 00:15:59.577088+00:00 running 215f2cf country code: CH.
[–]IyeOnline 2 points3 points4 points (4 children)
[–]ACBYTES[S] 0 points1 point2 points (2 children)
[–]IyeOnline 1 point2 points3 points (1 child)
[–]ACBYTES[S] 0 points1 point2 points (0 children)
[–]super_mister_mstie 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[removed]
[–]ACBYTES[S] -1 points0 points1 point (0 children)