Function as template argument in MSVC error

I stumbled upon strange behavior on MSVC 2015 RC that I cannot figure out for code that works fine on LLVM3.4.

I have a class hierarchy that includes some template classes that accept a non-member function as template arguments, so something like this (I've truncated all irrelevant code)

// .h file
namespace functions {
  float func1(float x) { return 0.0f; }
  float func2(float x) { return 0.0f; }
  float func3(float x) { return 0.0f; }
}

class Base
{
public:
  virtual Base* clone() const = 0;
  virtual ~Base() { }
};

using Func = float(*)(float);

template<Func F, Func I = F>
class Derived : public Base
{
public:
  Derived() { }
  ~Derived() { }

  static Derived<F,I>* create();
  Base* clone() const override;
};

using MyDerived = Derived<functions::func1>;
using MyDerived2 = Derived<functions::func2, functions::func3>;

// .cpp file
template<Func F, Func I>
Base* Derived<F,I>::clone() const
{  // <- all errors point here
  return new Derived<F,I>();
}

template<Func F, Func I>
Derived<F,I>* Derived<F,I>::create()
{
  return new Derived<F,I>();
}

template class Derived<functions::func1>;
template class Derived<functions::func2, functions::func3>;

      

Now this works fine on Clang, but on MSVC I got weird errors like

C2062: type 'float' unexpected
C2976: 'Derived': too few template arguments
C2955: use of class template requires template argument list
C2910: Derived<F,I>::clone: cannot be explicitly specialized

      

They don't look logical, especially the first error, so I'm wondering if it's an MSVC or Clang bug that allows something that shouldn't be resolved, even if I haven't found it yet.

These errors appear to have been generated from explicit template creation in the source file:

template class Derived<functions::func1>;
template class Derived<functions::func2, functions::func3>;

      

+3


source to share





All Articles