0

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

  • There is something very strange in your code. When you say "Idea was to specialize function for different NodeCachingOptions", what do you mean? You mean for different implementations of NodeCachingOptions or if NodeCachingOptions value is AddPath or DontAddPath? – Benjamin Barrois Jul 25 '18 at 14:39
  • Your first template function has a syntax issue anyway. You can't just give an existing class name in a template declaration like that. – Benjamin Barrois Jul 25 '18 at 14:40
  • CreateSObject should have different implementation for NodeCachingOptions::AddPath and NodeCachingOptions::DontAddPath. I don't want to write 2 different functions (e.g. CreateSObject_DontSavePath(…) and CreateSObject(...) ) – Dmitrii Motorygin Jul 25 '18 at 14:41
  • How is that different from e.g. template struct mul_impl <> ... template struct mul_impl ? (I looke here for example: https://stackoverflow.com/questions/2403928/partial-template-specialization-of-free-functions-best-practices) – Dmitrii Motorygin Jul 25 '18 at 14:42
  • Ok I understand what you want to do now (thanks to the answer below). I was thinking something totally different, my bad. – Benjamin Barrois Jul 25 '18 at 14:47

1 Answers1

4

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);

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85