Reputation: 1468
I want to pass the standardized C++ binary functions to a template function, but somehow I didn't get it to work.
The following is my attempt to do it:
template<template <typename> typename Pred,typename T, typename Iterator>
void iota_stepa(Iterator begin, Iterator end, T startofSequence_, T threadStep)
{
int currMaxThreads = startofSequence_;
bool first = true;
generate(begin, end, Pred<T>(currMaxThreads, threadStep) );
}
and testing it with:
vector<int> tempVect_(10, 0);
iota_stepa<std::plus>(begin(tempVect_),end(tempVect_),1,thread::hardware_concurrency());
gives me unfortunately the errors:
Severity Code Description Project File Line Suppression State
Error C2440 '<function-style-cast>': cannot convert from 'initializer list' to 'std::plus<int>'
Error C2672 'generate': no matching overloaded function found FractalCarpet
Error C2780 'void std::generate(_FwdIt,_FwdIt,_Fn0)': expects 3 arguments - 2 provided FractalCarpet
The console output looks like the following:
1> c:\users\mtunca\documents\esd\sps\fractalcarpet\main.cpp(55): note: see reference to function template instantiation 'void iota_stepa<std::plus,int,std::_Vector_iterator<std::_Vector_val<std::_Simple_types<float>>>>(Iterator,Iterator,T,T)' being compiled
1> with
1> [
1> Iterator=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<float>>>,
1> T=int
1> ]
1>c:\users\mtunca\documents\esd\sps\fractalcarpet\main.cpp(34): error C2672: 'generate': no matching overloaded function found
1>c:\users\mtunca\documents\esd\sps\fractalcarpet\main.cpp(34): error C2780: 'void std::generate(_FwdIt,_FwdIt,_Fn0)': expects 3 arguments - 2 provided
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm(1532): note: see declaration of 'std::generate'
Could someone help me, how to solve this problem?
Upvotes: 2
Views: 898
Reputation: 120051
Pred<T>(currMaxThreads, threadStep) );
Pred<T>
is a type. You need to construct an actual callable object:
Pred<T>()(currMaxThreads, threadStep) );
This however cannot be the last argument to std::generate
. The latter requires a callable object with no arguments, presumably holding a state (otherwise a call to std::fill woud suffice). It is unclear how an arbitrary binary function could be adapted to fill this role.
Upvotes: 0
Reputation: 7687
std::generate
needs a generator, something that can be called like gen()
.
You could create one with a lambda, perhaps like this:
template<template <typename> class Pred, typename T, typename Iterator>
void iota_stepa(Iterator begin, Iterator end, T startofSequence_, T threadStep)
{
bool first = true;
T current;
auto gen = [&]() -> T
{
if(first) {
current = startofSequence_;
first = false;
} else {
current = Pred<T>() ( current, threadStep );
}
return current;
};
generate(begin, end, gen );
}
Upvotes: 3