Zhilong Fang
Zhilong Fang

Reputation: 63

How to define a new function from an existed template function in C++

Recently I am learning the template function in C++. I am wondering if there is any simple way for me to do the following thing.

For example, I have defined a template function in C++ as follow:

template <typename T, float t_p>
void func_a(T* input, T* output):
{
       output = input / t_p;
}

Now, I want to define another template function based on this template function for f_p = 4.0. I know I may be able to do the following thing:

template <typename T>
void func_b(T* input, T* output):
{
      func_a<T,4.0>(input, output);
}

but this code looks very heavy. Especially when I have many input variables. I am wondering if there is any way that I can do to be similar as follows

template <typename, T>
func_b = func_a<T , 4.0>;

If so, it will be very helpful

Upvotes: 4

Views: 91

Answers (2)

Evg
Evg

Reputation: 26282

You can't do it with functions, but you can do it with functors. S.M. noted that you can't use float as a template non-type parameter, so let's replace it with int. I also suppose that you want to operate on values, not on pointers (either dereference pointers, or use references).

template<int t_p>
struct func_a
{
    template<typename T>
    void operator()(const T& input, T& output) const
    {
        output = input / t_p;
    }
};

using func_b = func_a<4>;
using func_c = func_a<5>;

Now you can use these functors in the following way:

void foo()
{ 
    int a = 100;
    int b;
    func_a<2>()(a, b);
    func_b()(a, b);
    func_c()(a, b); 
}

Note that you need extra empty parentheses to create a functor.

If you want to use float you can do something like this:

struct func_a
{
    func_a(float p) : p(p) { }

    template<typename T>
    void operator()(const T& input, T& output) const
    {
        output = input / p;
    }

private:
    const float p;
};

void foo()
{
    const auto func_b = func_a(4);
    const auto func_c = func_a(5);

    float a = 100;
    float b;
    func_a(2)(a, b);
    func_b(a, b);
    func_c(a, b);
}

Upvotes: 1

max66
max66

Reputation: 66200

Maybe a little Off Topic but I give you a useful, I hope, little suggestion: switch the order of your template parameters.

I mean: write func_a() as follows (I use int for t_p because, as pointed by S.M., a float value can't a valid template parameter)

template <int t_p, typename T>
void func_a(T* input, T* output):
 { output = input / t_p; }

The point is that T can be deduced from the function arguments (input and output) where t_p can't be deduced so must be explicated.

If the order is T first and t_p second, you must explicate also T, so (by example) in func_b() you must write

func_a<T,4>(input, output);

If the order is t_p first and T second, you must explicate only t_p and you can let the compiler deduce the T type; so you can simply write

func_a<4>(input, output);

In this case is a little improvement but, in other circumstances, can be useful.

Upvotes: 1

Related Questions