[英]Function template partial specialization - are there any workaround?
我有以下功能
enum class NodeCachingOptions
{
AddPath,
DontAddPath
};
template <typename T, NodeCachingOptions>
T* CreateSObject(const MPath& path)
想法是专门为不同的NodeCachingOptions提供功能。
原来不可能使用部分函数模板专门化,因此我尝试了一种解决方法:
template <typename T, NodeCachingOptions>
T* CreateSObject(const MPath& ob)
{
CreateSObject_Impl<class T, NodeCachingOptions> temp
return temp.call(ob);
}
template <typename T, NodeCachingOptions>
struct CreateSObject_Impl
{
T* call(const MPath& ob);
};
template <typename T>
struct CreateSObject_Impl<typename T, NodeCachingOptions::AddPath>
{
T* call(const MDagPath& ob)
{…}
}
template <typename T>
struct CreateSObject_Impl<typename T, NodeCachingOptions::DontAddPath>
{…}
但是我得到了编译错误::: NodeCachingOptions':非类型模板参数'__formal'的非法类型
我在做什么错,有没有更好的方法来解决这个问题?
我从这里开始了解struct impl: 自由函数的部分模板专业化-最佳实践
您的语法全错了。 做了
template <typename T, NodeCachingOptions opt>
T* CreateSObject(const MPath& ob)
{
CreateSObject_Impl<T, opt> temp;
return temp.call(ob);
}
您将类型NodeCachingOptions
的值作为NodeCachingOptions
的第二个模板参数CreateSObject_Impl
,而不是类型本身。
您可能想让call
成为CreateSObject_Impl
的静态成员,并写return CreateSObject_Impl<T, opt>::call(ob);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.