我有以下功能
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': illegal type for non-type template parameter '__formal'
我做错了什么,是否有更好的方法来解决这个问题?
我从这里想到了 struct impl:Partial template specialization of free functions - best practices
最佳答案
你的语法全错了。做到这一点
template <typename T, NodeCachingOptions opt>
T* CreateSObject(const MPath& ob)
{
CreateSObject_Impl<T, opt> temp;
return temp.call(ob);
}
您传递类型为 NodeCachingOptions
的值作为 CreateSObject_Impl
的第二个模板参数,而不是类型本身。
您可能想要制作 call
CreateSObject_Impl
的静态成员, 然后写 return CreateSObject_Impl<T, opt>::call(ob);
关于c++ - 函数模板部分特化 - 有什么解决方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51521520/