简体   繁体   中英

Function template partial specialization - are there any workaround?

I have the following function

enum class NodeCachingOptions
{
    AddPath,
    DontAddPath
};

template <typename T, NodeCachingOptions>
T* CreateSObject(const MPath& path)

Idea was to specialize function for different NodeCachingOptions.

Turned out it is impossible to use partial function template specialization, thus I tried a workaround:

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>
{…}

However I'm getting compile error: ::NodeCachingOptions': illegal type for non-type template parameter '__formal'

What am I doing wrong and is there a better way to solve this problem?

I took idea of struct impl from here: Partial template specialization of free functions - best practices

Your syntax is all wrong. Make it

template <typename T, NodeCachingOptions opt>
T* CreateSObject(const MPath& ob)
{
   CreateSObject_Impl<T, opt> temp;
   return temp.call(ob);
}

You pass the value of type NodeCachingOptions as the second template paramter of CreateSObject_Impl , not the type itself.

You may want to make call a static member of CreateSObject_Impl , and write return CreateSObject_Impl<T, opt>::call(ob);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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