3

With same template declaration, is it possible to differ two functions with same name, same param list, but different return type?

template <class T>
int f()...

template <class T>
short f()...

Or, need some special code to achieve this?

Thanks.

Troskyvs
  • 7,537
  • 7
  • 47
  • 115

1 Answers1

5

You can indeed have function templates with same name, same parameter types, and same return type (but you cannot for regular functions).

template <class T>
int f() {/*..*/}

template <class T>
short f() {/*..*/}

But then their usage is not really easy/fine:

auto i = static_cast<int(*)()>(&f<float>)(); // Call int f<float>
auto s = static_cast<short(*)()>(&f<float>)(); // Call short f<float>
walnut
  • 21,629
  • 4
  • 23
  • 59
Jarod42
  • 203,559
  • 14
  • 181
  • 302