Use the same template for several functions

SIMEL

If several functions which use the same template are needed, is there a way to declare and implement them without declaring the template each time?

template <typename T>
T mymax (T a, T b);
template <typename T>
T mymin (T a, T b);
template <typename T>
T myfoo (T a, T b);

/********************************************/
template <typename T>
T mymax (T a, T b) {
    return (a > b ? a : b);
}

template <typename T>
T mymin (T a, T b) {
    return (a < b ? a : b);
}
template <typename T>
T myfoo (T a, T b) {
    return max(a,b)+min(a,b);
}

Is there a way to write the line template <typename T> just one time for a block of code? Something that will look like:

template <typename T> {
  T mymax (T a, T b);
  T mymin (T a, T b);
  T myfoo (T a, T b);
}

(this code isn't legal syntax and doesn't compile)

rubenvb

The only way to achieve something like this is to misuse a struct and static functions. Unfortunately, you'll need to explicitly mention the template type.

#include <iostream>

template<typename T>
struct my
{
  static T max(T a, T b) { return (a > b ? a : b); }
  static T min(T a, T b) { return (a < b ? a : b); }
  static T foo(T a, T b) { return max(a, b) + min(a, b); }
};

Live Demo. Pick a better class name.

I can't think any "better" solution. Just write the template<typename T>. You'll get used to it. It serves a purpose, and it really isn't as ugly as you think.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Use the same template for more functions

From Dev

Use the same template for more functions

From Dev

Call several functions with the same value

From Dev

Use same hotkey in several workbooks

From Dev

Multiple functions using the same template?

From Dev

Multiple functions using the same template?

From Dev

Invoke several functions to the same variable in JavaScript

From Dev

Test several functions with the same list of value with quickCheck

From Dev

call several functions within same class in hooks

From Dev

Use of lapply with custom functions of several arguments

From Dev

How to use std functions in template?

From Dev

Use generalized template class in specialized template functions

From Dev

Use generalized template class in specialized template functions

From Dev

Is it OK to use same keystore for several apps?

From Dev

Let several modules use the same mongo instance

From Dev

Is it OK to use same keystore for several apps?

From Dev

Factory Girl: Use same object several times

From Dev

Use the same template on different scopes

From Dev

how to rerender a template several times for different things at the same time

From Dev

How to call the same functions to several buttons c#

From Dev

How to use jQuery built in functions in Mustache template

From Dev

use c++ template to generate JNI functions

From Dev

Use arbitrary functions as template parameter arguments

From Dev

use c++ template to generate JNI functions

From Dev

Is it possible to use a class template in its member functions?

From Dev

Blade template. Understanding manual or examples for use with several sections

From Dev

Blade template. Understanding manual or examples for use with several sections

From Dev

Wrap several ggplot2 functions and use the wrapper with operator "+" in R

From Dev

How can several HTML elements be referred to, for use in javascript/jQuery functions?

Related Related

  1. 1

    Use the same template for more functions

  2. 2

    Use the same template for more functions

  3. 3

    Call several functions with the same value

  4. 4

    Use same hotkey in several workbooks

  5. 5

    Multiple functions using the same template?

  6. 6

    Multiple functions using the same template?

  7. 7

    Invoke several functions to the same variable in JavaScript

  8. 8

    Test several functions with the same list of value with quickCheck

  9. 9

    call several functions within same class in hooks

  10. 10

    Use of lapply with custom functions of several arguments

  11. 11

    How to use std functions in template?

  12. 12

    Use generalized template class in specialized template functions

  13. 13

    Use generalized template class in specialized template functions

  14. 14

    Is it OK to use same keystore for several apps?

  15. 15

    Let several modules use the same mongo instance

  16. 16

    Is it OK to use same keystore for several apps?

  17. 17

    Factory Girl: Use same object several times

  18. 18

    Use the same template on different scopes

  19. 19

    how to rerender a template several times for different things at the same time

  20. 20

    How to call the same functions to several buttons c#

  21. 21

    How to use jQuery built in functions in Mustache template

  22. 22

    use c++ template to generate JNI functions

  23. 23

    Use arbitrary functions as template parameter arguments

  24. 24

    use c++ template to generate JNI functions

  25. 25

    Is it possible to use a class template in its member functions?

  26. 26

    Blade template. Understanding manual or examples for use with several sections

  27. 27

    Blade template. Understanding manual or examples for use with several sections

  28. 28

    Wrap several ggplot2 functions and use the wrapper with operator "+" in R

  29. 29

    How can several HTML elements be referred to, for use in javascript/jQuery functions?

HotTag

Archive