简体   繁体   English

具有前向声明类型的函数模板专业化

[英]Function Template Specialization with Forward Declared Type

Haven't been able to quite find a duplicate. 一直没能找到重复的。

Is it possible to forward-declare the type used in a function specialization? 是否可以转发声明函数特化中使用的类型?

Consider the following code: 请考虑以下代码:

in .h 在.h

template <typename T>
T* Foo()
{
    //generic implementation
}

template<>
class SpecialT* Foo<class SpecialT>();

in .cpp 在.cpp

#include "SpecialT.h"

template<>
SpecialT* Foo<SpecialT>()
{
    //specialized implementation
}

Is there any syntax in which the above is possible and doesn't result in a slew of compiler errors (C2910, C2909, C2768, etc.)? 是否存在上述可能的语法,并且不会导致大量编译器错误(C2910,C2909,C2768等)?

This of course compiles if "SpecialT.h" is included in the template header. 如果模板头中包含“SpecialT.h”,这当然会编译。

The simple workaround is to simply put the forward declaration on its own line: 简单的解决方法是简单地将前向声明放在自己的行上:

class SpecialT;
template<>
SpecialT* Foo<SpecialT>();

VS 2015 on Godbolt accepts it just fine. Godbolt的VS 2015接受它就好了。

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

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