简体   繁体   English

单独实施部分专业化的模板

[英]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 . 为此,我使用了在这些SO问题中也提到的技术:类函数签名的表达式作为C ++模板参数,函数签名作为模板参数

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. 这样可以很好地工作,并用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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