Search code examples
c++templatescode-duplicationdecltypeexplicit-instantiation

Can I use decltype() to avoid code duplication in explicit template instantiations?


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.


Solution

  • 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.