0

I want to be able to do the equivalent of this:

template <class T>
void foo(int i=42) {
  // ... do stuff and use T for something ...
}

using ifoo= foo<int>; // <-- This is the part I am looking for

int main() {
  ifoo();
}

Is there any way to do something like this?

Baruch
  • 20,590
  • 28
  • 126
  • 201
  • `auto ifoo = &foo;`? AFAIK, there are no aliases for functions (nor function template instances) but you still can store a pointer to a function. – Scheff's Cat Jun 01 '22 at 14:36
  • @Scheff'sCat "there are no aliases for functions". A function pointer is exactly what one would expect from an alias, no? – 463035818_is_not_an_ai Jun 01 '22 at 14:37
  • @463035818_is_not_a_number I wouldn't dare to call a function pointer an "alias"... ;-) Maybe, I was too focused on alias like in `using` or `typedef`. – Scheff's Cat Jun 01 '22 at 14:38
  • 1
    @463035818_is_not_a_number Have a look at the Definition at the bottom of the question in [C++11: How to alias a function?](https://stackoverflow.com/q/9864125/7478597). (I don't know whether it's correct or authoritative but this is what I had in mind.) – Scheff's Cat Jun 01 '22 at 14:45
  • @Scheff'sCat You are right that it is not an alias. One obvious difference is that if `foo()` has default parameters, `ifoo()` will not. Maybe my example was to simple. – Baruch Jun 01 '22 at 18:50

3 Answers3

1

You can use a function pointer:

auto ifoo = &foo<int>;

Note that the & is optional (ie you can also write auto ifoo = foo<int>;). auto gets deduced as void (*)().

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 2
    I would mark it `const` or `constexpr`. Otherwise a call to it cannot be inlined and a function shouldn't be assignable either. – user17732522 Jun 01 '22 at 15:10
  • While this is good (and this is what I did in my code), `ifoo` doesn't inherit the default parameters of `foo`, so if it is declared as `foo(int i=42);`, I will have to call `ifoo` with a parameter – Baruch Jun 01 '22 at 18:52
  • @Baruch in your question `foo` has no arguments. Anyhow, then I'd go for Mareks solution – 463035818_is_not_an_ai Jun 01 '22 at 18:53
1

The simplest solutions are usually the best:

void ifoo()
{
   foo<int>();
}

compiler should optimize this so ifoo and foo<int> just shares code.

Marek R
  • 32,568
  • 6
  • 55
  • 140
0

you can create a reference (alias) to the specific function.

auto& ifoo= foo<int>;

or alias the whole template (technically one-by-one)

template <typename T>
auto& fooT = foo<T>;

https://godbolt.org/z/nqbKfnfhe (with some more examples)

apple apple
  • 10,292
  • 2
  • 16
  • 36
  • Interesting, `auto& fn = func;` works, but `auto& fn = &func;` does not. (`auto const& fn = &func;` works for the second case.) – Eljay Jun 01 '22 at 15:06
  • 1
    @Eljay `&func` is a non-`const` pointer prvalue. A non-`const` reference cannot be bound to a prvalue. In `auto& fn = func;` the initializer is a lvalue to which the reference binds directly. (No implicit function-to-pointer conversion takes place.) – user17732522 Jun 01 '22 at 15:14
  • @user17732522 • Thank you, I get it. I'm just so used to function pointer decay that it startles me when there's a difference. – Eljay Jun 01 '22 at 15:16
  • 1
    @Eljay you can also bind to rvalue reference `auto&& f = &func` – apple apple Jun 01 '22 at 16:12
  • or `void (*&&f)() = &func` (in both case it's binding to a pointer to the function) – apple apple Jun 01 '22 at 16:19