19

What I'd like to do is something like this:

template <class DataType>
DataType myFunc(DataType in)
{
   ...
}

typedef myFunc<int> myFunc_i;

myFunc_i(37);

...however, typedefs cannot be used for functions like this in C++. What I was wondering is... what are people's preferred alternatives in this case? The only ones I can think of are:

1) Just deal with it, and always use myFunc syntax 2) Manually create a wrapper function, ie

inline int myFunc_i(int in)
{
    return myFunc<int>(in);
}

This would work, but would have the downside of requiring extra maintenance, and the possibility that it would get out of sync (ie, if you change the function signature for myFunc).

Thoughts?

Paul Molodowitch
  • 1,366
  • 3
  • 12
  • 29

5 Answers5

14

Try this:

typedef int (*myFunc_i_type)(int);
myFunc_i_type const myFunc_i = &myFunc<int>;

This will trigger a compiler error if the signature of myFunc ever changes. Making it const also prohibits reassigning. Function pointers are also callable like any normal function: myFunc_i(5).

If you don't want a compiler error but rather have it update itself, use auto (again with const):

auto const myFunc_i = &myFunc<int>;
Xeo
  • 129,499
  • 52
  • 291
  • 397
10

Some may disagree, some may jerk their knees against this, but consider this option:

#define myFunc_i myFunc<int>

As far as macros go, this one is quite safe; the only danger is that someone could redefine or undefine it later.

Aside from that, it has all the advantages of auto const myFunc = myFunc<int>;. If you want auto but don't want to use C++0x features yet, this is a more portable way to achieve the same effect.

Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
  • 2
    That's... honestly, one of the best uses I've seen for preprocessor macros in C++. It can work well with C99/C++11 variadic macros, too, such as if you want to be able to explicitly specify function template parameters without having to type them out each time. For example, `#define myFunc_allSameType(a, ...) myFunc(a, __VA_ARGS__)` on MSVC or `#define myFunc_allSameType(a, ...) myFunc(a, ##__VA_ARGS__)` on GCC. Not always useful, but helpful in cases where there are a lot of template parameters, and they're all either known beforehand or determined from arguments. – Justin Time - Reinstate Monica Feb 03 '16 at 22:41
  • This *is* a real answer. Other's are using external variables, which is not a kind of "typedef"s. – 김선달 Apr 27 '20 at 01:46
7

Code Example:

#include <iostream>


template <typename T>
T sqr(T a){
    return a*a;
}

auto s = sqr<int>;
int main() {

    std::cout<<s(3.9);
    return 0;
}

prints 9, that means that used int function
auto requires c++0x, you may use real typename instead, but it's ugly and you'll need check changing signature too. Besides, this code, may disallow your compiler to inline this function

  • As for me, it's often better use full name with <>

  • In your case you need not use <> because myFunc is enough(DataType may be got from argument type)

RiaD
  • 46,822
  • 11
  • 79
  • 123
4

I would make a just tiny improvement over Xeo's answer.

Instead of using:

auto const myFunc_i = &myFunc<int>;

I would use

constexpr auto myFunc_i = &myFunc<int>;
Juan Leni
  • 6,982
  • 5
  • 55
  • 87
0

Use decltype:

template <class DataType>
DataType myFunc(DataType in)
{
   ...
}

using myFunc_i = decltype(myFunc<int>(0));

myFunc_i(37);
jtooker
  • 1,043
  • 1
  • 8
  • 22
  • 4
    This doesn't work. It is equivalent to "using myFunc_i = int;". See RiaD's answer for the correct version. – alwaysmpe Oct 04 '18 at 15:15