簡體   English   中英

功能模板部分專業化-有解決方法嗎?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM