1

i have been working on a project and i need to know that is there anyway passing a function to template class? lets say i want this to perform sinus or cosinus function

#ifndef INTEGRAL_H
#define INTEGRAL_H

template<class F>
class Integral
{
    public:
        Integral( F func):f_(func){}
        ~Integral()=default;
         Integral(){
            int a,b,N;
            double h=(b-a)/N;
            sum=0.0;
            for (size_t j=0; j<N;++j)
                {
                sum+=f_(a+j*h); // here i would like to perform sin(a+j*h) or cosinus or somethingelse
                }

            sum*=h;

        }

        double Sum(){return sum;}

    private:
        F f_;
        double sum;

};

#endif // INTEGRAL_H
  • I would recommend that you pass a [`std::function`](https://en.cppreference.com/w/cpp/utility/functional/function) object instead. No templates needed. – Some programmer dude Nov 15 '19 at 15:23
  • 1
    Your code should work totally fine as-is. Can you elaborate what the problem is? – Max Langhof Nov 15 '19 at 15:25
  • Voting to close as unclear what the question is and how the posted code relates to it.# – underscore_d Nov 15 '19 at 15:26
  • @MaxLanghof, the problem might be that `std::sin` is not a function, but a template. – Evg Nov 15 '19 at 15:27
  • 1
    @Evg Then that would be good to have in the question (preferably from the start, seeing as it would invalidate the current answer). – Max Langhof Nov 15 '19 at 15:28
  • Yes, there's no point posting a vague goal and some code and no context on how they relate and why the code doesn't work. – underscore_d Nov 15 '19 at 15:31
  • it gives error when i initialize as Integral myintegral; or any other type(ddouble,int as template parameter) but i may use https://en.cppreference.com/w/cpp/utility/functional/function here if nothing happens i will ask again, thanks so much – Lokman Tasbas Nov 15 '19 at 15:31
  • 1
    do you really need to store the function as member? I would rather make the function type not part of the type of the `Integral` but rather let it accept any callable to be integrated – 463035818_is_not_an_ai Nov 15 '19 at 15:37
  • @LokmanTasbas "it gives error" is not a problem description. What error? For what line? [Edit] your post to quote it in full. – underscore_d Nov 15 '19 at 15:38
  • 1
    @formerlyknownas_463035818, the whole class looks superfluous. – Evg Nov 15 '19 at 15:50

3 Answers3

3

Your code works "as coded" (after a small fix in the constructor, unrelated to the question). But std::sin doesn't name a single function, and you can't pass it into Integral directly. The simplest way is to wrap it into a lambda:

Integral i([](auto arg) { return std::sin(arg); });
double value = i.Sum();

If your compiler doesn't support template argument deduction (available since C++17), you have to be more verbose:

auto my_sin = [](auto arg) { return std::sin(arg); };
Integral<decltype(my_sin)> i(my_sin);
double value = i.Sum();
Evg
  • 25,259
  • 5
  • 41
  • 83
1

This might help: Function passed as template argument

In the end, a function is nothing but an address to a particular place in memory which boils down to a simple number and that is something that the template system supports.

However, in your case I would recommend just passing an std::function object which is a glorified function pointer as a regular parameter to your function - omitting the need for a template (at least for this part of the functionality).

Joel Bodenmann
  • 2,152
  • 2
  • 17
  • 44
1

is there anyway pass function as template parameter?

Yes. Example:

template<return_type (template_argument_name)(argument_list)>

Another approach is to pass a callable type as template parameter, and invoke an object of that type. This is in fact what you've done in your example.

eerorika
  • 232,697
  • 12
  • 197
  • 326