It's my first post here so plese be kind if I don't respect the "ways and customs" :)
I'm new using Boost::Phoenix and I want to pass a function to a methods defined like :
template <typename Selector>
Result Greedy (
const t_Capacity& capacity,
BTSSet stations,
BTSSet startSet)
{
//...
function <Selector> sel;
while ( !stations.empty() ) {
BTSSet::iterator currentStation = sel(stations);
// ...
}
// ...
}
My selector function is :
struct rouletteWheelSelector {
typedef BTSSet::iterator result_type;
BTSSet::iterator operator () ( const BTSSet& stations ) {
// ...
}
};
But my compiler says there's no way to convert from from 'typename detail::expression::function_eval<rouletteWheelSelector, set<BTS *, BTS_Cmp, allocator<BTS *> > >::type const'
to BTSSet::Iterator.
Is my functor declaration ok ? How can I force the compiler to deduce the right return type of sel ?
Thank you !
You have three issues:
boost::phoenix::function<>
is lazy, so it must be evaluated twice in order to get an actual result.rouletteWheelSelector::operator()
must be const in order to be used byboost::phoenix::function<>
.sel
is capturingstations
by value, and consequently returning an iterator into a destroyed set; useboost::phoenix::cref
to capturestations
by const-reference.This code compiles and runs cleanly for me with VC++ 2010 SP1 and Boost 1.47.0:
If you're using Phoenix v2 rather than Phoenix v3, as @jpalecek correctly noted in his now-deleted answer, you must use a nested
result<>
template inside ofrouletteWheelSelector
rather thanresult_type
:However, all that said, why are you using
boost::phoenix::function<>
at all here? For your usage,Greedy<>
could more easily (and efficiently) be implemented without it: