How can I create a vector based on a variable type

I'm using a template function, which the goal is reciever a vector and a function, and return the function type.

template <typename T, typename Function>
auto apply(const std::vector<T>& V, const Function &F){
    vector<Function> x; # ERROR HERE
    return x;
}

But the IDE give me error (http://coliru.stacked-crooked.com/a/ee6ce2127e013a18):

/usr/local/include/c++/10.2.0/ext/new_allocator.h: In instantiation of 'class __gnu_cxx::new_allocator<double(double)>':
/usr/local/include/c++/10.2.0/bits/allocator.h:116:11:   required from 'class std::allocator<double(double)>'
/usr/local/include/c++/10.2.0/bits/stl_vector.h:87:21:   required from 'struct std::_Vector_base<double(double), std::allocator<double(double)> >'
/usr/local/include/c++/10.2.0/bits/stl_vector.h:389:11:   required from 'class std::vector<double(double), std::allocator<double(double)> >'
main.cpp:10:22:   required from 'auto apply(const std::vector<T>&, const Function&) [with T = int; Function = double(double)]'
main.cpp:19:39:   required from here
/usr/local/include/c++/10.2.0/ext/new_allocator.h:96:7: error: 'const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = double(double); __gnu_cxx::new_allocator<_Tp>::const_pointer = double (*)(double); __gnu_cxx::new_allocator<_Tp>::const_reference = double (&)(double)]' cannot be overloaded with '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = double(double); __gnu_cxx::new_allocator<_Tp>::pointer = double (*)(double); __gnu_cxx::new_allocator<_Tp>::reference = double (&)(double)]'
   96 |       address(const_reference __x) const _GLIBCXX_NOEXCEPT
      |       ^~~~~~~
/usr/local/include/c++/10.2.0/ext/new_allocator.h:92:7: note: previous declaration '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = double(double); __gnu_cxx::new_allocator<_Tp>::pointer = double (*)(double); __gnu_cxx::new_allocator<_Tp>::reference = double (&)(double)]'
   92 |       address(reference __x) const _GLIBCXX_NOEXCEPT
      |       ^~~~~~~
main.cpp: In function 'int main(int, char**)':
main.cpp:19:31: error: conversion from 'vector<double(double),allocator<double(double)>>' to non-scalar type 'vector<double,allocator<double>>' requested
   19 |     vector<double> r = ::apply(v, seno);
      |                        ~~~~~~~^~~~~~~~~

This is call of the main function.

double seno( double n ) { return sin(n); }

int main( int argc, char* argv[]) {

    vector<int> v{ 1, 2, 3, 4, 5 };
    vector<double> r = ::apply(v, seno);
    cout << r;

    return 0;
}

I don't know what I'm doing wrong, so How can I improve this method and pass trough this error?

EDIT: The purpse to generalize the in method insted of using double in the vector is because I want o re-use in another way. So I've generalize the most that I can.



Read more here: https://stackoverflow.com/questions/67013219/how-can-i-create-a-vector-based-on-a-variable-type

Content Attribution

This content was originally published by xxxprincemaker at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: