I have the following code of templated zip function:
template< typename T1, typename T2, typename R, R F( const T1, const T2)>
inline std::vector< R> zip( const std::vector< T1> & v1, const std::vector< T2> & v2) {
if( v1.size() != v2.size())
throw exception( "Bad length!");
typename std::vector< T2>::const_iterator it2 = v2.begin();
std::vector< R> res;
for( typename std::vector< T1>::const_iterator it1 = v1.begin();
it1!= v1.end();
++it1)
{
res.push_back( F( *it1, *it2));
++it2;
}
return res;
}
and I'm trying to use it like this:
class C {
public:
static C* foo( const C* c1, const C* c2) {
return new C;
}
};
std::vector< C*> sum( const std::vector< C*> &v1, const std::vector< C*> &v2)
{
return zip< C*, C*, C*, C::foo>( v1, v2);
}
getting:
error: could not convert template argument 'C::foo' to 'C* (*)(C*, C*)'
What should I do to make it compile and execute?
You're not passing a type to the template list:
zip< C*, C*, C*, C::foo>( v1, v2);
^^^^^^
Try this code
template< typename T1, typename T2, typename R, typename F>
inline std::vector<R> zip(const std::vector<T1> & v1,
const std::vector<T2> & v2, F f) {
//...
res.push_back( f( *it1, *it2));
//...
}
//...
std::vector<C*> sum(const std::vector< C*> &v1, const std::vector< C*> &v2)
{
return zip< C*, C*, C*>( v1, v2, C::foo);
}
See here for a working code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.