1

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)

SIMEL
  • 8,745
  • 28
  • 84
  • 130
  • No. Wait for concepts. – Yakk - Adam Nevraumont Nov 12 '14 at 19:50
  • Only thing that comes to mind is making them all static functions in a class template. But then you need `ClassName::mymax()` everywhere, so you haven't really gained much. – dlf Nov 12 '14 at 19:50
  • `myfoo` is a really convoluted way to write `mysum`... – T.C. Nov 12 '14 at 21:45
  • @T.C. All of these functions are just dummy functions that I wrote for testing and to give as an example in the question. `myfoo` is an example for a `template` function that has other `template` functions embedded inside. – SIMEL Nov 12 '14 at 21:51
  • Since no-one else is saying it I'll say it: Macro. – Daniel Frey Nov 12 '14 at 22:08
  • @DanielFrey, please explain and demonstrate. – SIMEL Nov 13 '14 at 06:02
  • @IlyaMelamed I found a better solution/option, see my answer. But if you really want to know about macros: `#define MYFUNC(NAME) template T NAME(T a, T b)` and use `MYFUNC(mymin); MYFUNC(mymax); MYFUNC(myfoo);`. – Daniel Frey Nov 13 '14 at 06:06

2 Answers2

1

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.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • [My question about this was marked a duplicated](http://stackoverflow.com/questions/30320369/alias-of-a-function-template). But your answer does not really solve my problem. I need the declarations and definitions to be separate. Your solution still requires writing out the declaration for each except without the template. That's still redundant. I don't mean to criticize but can't the preprocessor be used to do this better? – Z boson May 20 '15 at 11:38
  • @Zboson I reopened your question as it is indeed not a duplicate. But please rephrase/expand on your issue. As it stands, it is very unclear what you mean... – rubenvb May 20 '15 at 11:54
0

There is an option to make the code shorter, but it is actually the exact opposite of what you are asking for: You can not leave out the template<...>-part, but you can simplify the rest:

// create a shortcut for the function type
template<typename T>
using my = T( T a, T b );

// declare several functions with an identical signature
template<typename T> my<T> mymin;
template<typename T> my<T> mymax;
template<typename T> my<T> myfoo;

Note that this works for the declarations only, the definitions will not benefit from this.

Live example

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180