Reputation: 7319
I'm trying to pass this auto function to another function, but apparently I can't pass an auto type, so can I change it to whatever the type actually is in order to pass it? By googling I read that you can use templates, but we didn't go over them in my class yet so I'd rather find a more beginner way of doing it if there is one.
std::uniform_int_distribution<int> yPosition(lowBounds, 23);
auto randY = std::bind(yPosition, std::ref(engine));
void my_function(??? randY)
{
}
Upvotes: 2
Views: 298
Reputation: 3890
As the other answers state you need to use a template if you want the function to take the exact same type that falls out of the bind.
However if you just want a way to call the bound function inside of my_function
you can use std::function<>
. std::function
uses type-erasure to abstract away the full type, leaving you with a generic way to call whatever function-like type you put in it. Of course std::function
incurs a (small) run-time overhead.
void my_function(std::function<int()> randY) {
//...
int y = randY();
//...
}
Upvotes: 2
Reputation: 63124
From std::bind
:
template <class F, class... Args> /*unspecified*/ bind( F&& f, Args&&... args );
The type you're looking for is unspecified. It is, of course, an actual type, but it's whatever your standard library chose, and not something you can portably spell out. And even if you could, it's probably an horrendeous mess of nested meta-templates which you wouldn't want to spell out.
Thanks, auto
!
Your only solution is to make my_function
a template :
template <class RandY>
void my_function(RandY &&randY) {...}
Upvotes: 8
Reputation: 302817
The way to pass an auto type is to use templates. There is no alternative†
template <typename F>
void my_function(F randY)
{ ... }
my_function(auto randY)
, but that is just a proposal.
Upvotes: 2