user5406764
user5406764

Reputation: 1795

Out-of-line variadic template function

I have a need to separate the declaration from the definition of a variadic template function:

struct A
{
    // In-line declaration of function
    template<typename...Args>
    A(Args&&...args);
};

// Out-of-line definition of function
template<typename...Args>
A<Args...>::A(Args&&...args)
{}

int main()
{
    A a("hello");
    return 0;
}

...here's the error I get (Clang 3.9 OS X 10):

main.cpp:8:2: error: expected unqualified-id
A<Args...>::A(Args&&...args)
 ^

Do I need to put 'typename' somewhere? Thanks for help in advance!

Upvotes: 1

Views: 177

Answers (1)

WhiZTiM
WhiZTiM

Reputation: 21576

You wrote:

template<typename...Args>
A<Args...>::A(Args&&...args)
{}

But your class isn't a class-template, so below is how you do it

template<typename...Args>
A::A(Args&&...args)
{}


Even if it was a class-template, the example below is how you deal with class-template's templated constructor

template<typename... T>
struct A
{
    // In-line declaration of function
    template<typename...Args>
    A(Args&&...args);
};

// Out-of-line definition of function
template<typename... T>
template<typename... Args>
A<T...>::A(Args&&...args)
{}

Upvotes: 6

Related Questions