Reputation: 628
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?
Upvotes: 0
Views: 52
Reputation: 118300
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;
}
Upvotes: 1