5

For example

template<class T, class U>
void f();

template<class T> using g = f<T, int>;

Or any similar idea for functions?

uchar
  • 2,552
  • 4
  • 29
  • 50
user1899020
  • 13,167
  • 21
  • 79
  • 154
  • 5
    Why don't you try it? – Tim Seguine Jun 25 '14 at 17:06
  • tried. failed. wondering any workaround or new c++ features to support that. – user1899020 Jun 25 '14 at 17:11
  • 3
    Then tell us that. Telling us what you tried(with perhaps even error messages), along with what you are trying to do and why are important for a good question. – Tim Seguine Jun 25 '14 at 17:17
  • I actually think this is a good question. He’s looking for something similar to `using` that works for functions, which is fine IMO. –  Jun 25 '14 at 17:21
  • @rightfold I think there is a good question hiding there if it were asked better. To be clear: without the information in the comment, this clearly "does not show any research effort" – Tim Seguine Jun 25 '14 at 17:22
  • 1
    @TimSeguine, I knew what this question was about the second I saw it. If you didn't, that is your problem, but the question is crystal clear to anyone who ever had this kind of a problem. Sure, "how do I alias a function?" would possibly be a better question, but by no means this is "no research effort", it's "how do I do this?" asking about a problem many of us have hit, since there's no real, full solution that doesn't involve lots of boilerplate. – Griwes Jun 25 '14 at 17:33
  • 2
    @TimSeguine “Why don’t you try it?” is rarely a good idea with C++ (non-conforming implementations, UB, you name it). And even then, the question mentions “any ___similar___ idea” which you cannot magically think of. –  Jun 25 '14 at 17:36
  • related : [aliasing a variadic template function](http://stackoverflow.com/questions/24330300/aliasing-a-variadic-template-function) – uchar Jun 25 '14 at 18:41
  • Whoa, whoa. I never said it was a bad question. I think it is a question that deserves answering. I said it "looks" like no research. If the OP did research (which they obviously did) then they should mention that in the post, period. I think it can and should be phrased better. Reason being: I can't imagine this coming up in a Google result if I searched for this same problem. Even great answers aren't that helpful if nobody else can find them. And just so it is clear, I am not one of the downvoters. – Tim Seguine Jun 26 '14 at 18:33

2 Answers2

9

No. You cannot do that. You need to create a new function that calls f, forwarding all arguments and template arguments.

template<class T, class U>
void f();

template<class T>
void g() {
    f<T, int>();
}

A C++14 alternative is a variable template of function pointer type:

template<typename T>
void (*g)() = &f<T, int>;

Although this approach ignores default arguments and probably has other quirks as well. I highly recommend the more verbose wrapping approach.

2

No, you can not do that as templated aliases are used to create aliases of types, not concrete data.

What you are trying to do is to create an alias/type from the address of a function as f<T, int> decays into a pointer to function.

You can however create a templated alias from the type of the function f.

template <typename T>
using g = typename std::add_pointer<decltype(f<T, int>)>::type;

int main() {
    // Type of 'func' is 'void (*)()' with template arguments '{T1=double, T2=int}'.
    g<double> func;

    func = f<double, int>; // Point to function with same signature and template args.
    func();                // Ok to call function.
}
Felix Glas
  • 15,065
  • 7
  • 53
  • 82