简体   繁体   中英

Separate implementation of a partially specialized template

I'm creating a template for a class which is supposed to be taking a function signature as template argument (a factory class for that signature, in fact).

To do that I'm using the technique which is also mentioned in these SO question: Function signature-like expressions as C++ template arguments and Function signature as template parameter .

This works perfectly fine as long as I have the declaration within the specialization definition:

template<typename Signature>
class Factory;

template<typename Ret, typename... Args>
class Factory<Ret(Args...)>
{
public:
Factory(){}
};

Now I wanted to separate the declaration after the definition to have the header more readable for the user but I'm unable to find the right signature for that.

I tried the following signatures

template<typename Ret, typename... Args>
Factory<Ret, Args...>::Factory(){}

template<typename Ret, typename... Args>
Factory<Ret(Args...)>::Factory(){}

But in both cases the compiler is trying to define that function for the generic template rather than the specialization and that obviously fails.

Do you have any idea how to define the function of a partially specialized class template of this type?

The second signature is the correct one.

This works perfectly fine, and produces the expected results with gcc.

#include <iostream>

template<typename Signature>
class Factory;

template<typename Ret, typename... Args>
class Factory<Ret(Args...)>
{
public:
    Factory();
};


template<typename Ret, typename... Args>
Factory<Ret(Args...)>::Factory()
{
    std::cout << "Hello world" << std::endl;
}

int main()
{
    Factory< int(char, float) > foo;
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM