c++ - 函数模板部分特化 - 有什么解决方法吗?

标签 c++ templates

我有以下功能

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/

相关文章:

c++ - 如何使用模板设置 union 字段

c++ - 你如何检查 C++ 中 double 的长度

c++ - 在 Ubuntu 16.04 LTS 中为 OpenCV 使用 CodeLite IDE

c++在编译时加载大量数据

c++ - 为什么不允许使用 0 作为 void* 的默认非类型模板参数

c++ - 某些输入的展平/重构 C++ 模板源

c++ - 指向成员函数怪异的指针

c# - 将 native C++ 移植到 .NET

html - 多分辨率布局

c++ - 如何使用模板参数成员的类型?