6

I have a long template function declaration:

template <typename T> void foo(lots ofargs, goin here, andeven more, ofthese arguments, they just, dont stop);

with no overloads. and I want to explicitly instantiate it. I can write (say for T = int):

template void foo<int>(lots ofargs, goin here, andeven more, ofthese arguments, they just, dont stop);

But I really don't want to copy that long declaration. I would have liked to be able to say something like:

template <typename T> using bar = decltype(foo<T>);

and then:

template bar<int>;

Now, the first line compiles (GCC 4.9.3), but the second line doesn't. Can I make it work somehow? Or can I use decltype() some other way to avoid copying the declaration for the instantiation?

Note: I intentially used an example in which you can't deduce the type from just the arguments, since I want any solution to support this case as well.

einpoklum
  • 118,144
  • 57
  • 340
  • 684

1 Answers1

3

Sure. From [temp.explicit]:

The syntax for explicit instantiation is:
    explicit-instantiation:
        externopt template declaration

[...] If the explicit instantiation is for a function or member function, the unqualified-id in the declaration shall be either a template-id or, where all template arguments can be deduced, a template-name or operator-function-id. [ Note: The declaration may declare a qualified-id, in which case the unqualified-id of the qualified-id must be a template-id. —end note ]

We need a declaration. Let's pretend we're starting with:

template <class T> void foo(T ) { }

We can explicitly specialize via just:

template void foo<char>(char );   // template-id
template void foo(int );          // or just template-name, if the types can be deduced

This is the same as having written:

using Fc = void(char );
using Fi = void(int );

template Fc foo<char>;
template Fi foo;

Which is the same as having written:

template <class T> using F = decltype(foo<T> );

template F<char> foo<char>;
template F<int> foo;

Basically, the reason that template bar<int> doesn't work is that it's not a declaration. You need the name too.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • I'm editing the question so as to make sure you can't deduce the template argument for the function arguments, which is what I was actually interested in. I just sprinkled a T t for good measure, didn't mean for you to use it... If your answer is still relevant, then great. – einpoklum Apr 11 '16 at 21:10
  • @einpoklum That isn't remotely clear from the question, and why do you think my answer isn't relevant? – Barry Apr 11 '16 at 21:19
  • I didn't say it was irrelevant, I just had the sense (before your last edit) that you might be relying on it more than you actually are. – einpoklum Apr 11 '16 at 21:26