slowstart
slowstart

Reputation: 333

Member template function definition outside class fails to build Visual studio 2013

I have pasted below the bare minimum of a complex piece of template code that I am dealing with.

 1  template <typename T>
 2  class Base
 3      : public T
 4  {
 5  public:
 6      template <typename W, W& (T::ToImpl::*Func)()>
 7      bool Foo();
 8  };
 9  
10  template <typename T>
11  template <typename W, W& (T::ToImpl::*Func)()>
12  bool Base<T>::Foo()
13  {}
14  
15  int
16  main()
17  {
18      return 0;
19  }

The code is pretty straight forward, so I won't explain anything. I am unable to compile this code with Visual Studio 2013 (aka VC++12). It gives the following error:

main.cc(13): error C2244: 'Base<T>::Foo' : unable to match function definition to an existing declaration
          definition
          'bool Base<T>::Foo(W)'
          existing declarations
          'bool Base<T>::Foo(W)'

Out of curiosity I tried compiling the above code with g++ (4.4.7) and it compiled fine.

I would appreciate if someone can offer an explanation on why the code fails to compile on windows? A fix would be even sweeter. :)

Upvotes: 1

Views: 179

Answers (1)

Axalo
Axalo

Reputation: 2953

This should work:

template <typename T>
struct to_impl
{
    typedef typename T::ToImpl type;
};

template <typename T>
class Base
    : public T
{
public:
    template <typename W, W& (to_impl<T>::type::*Func)()>
    bool Foo();
};

template <typename T>
template <typename W, W& (to_impl<T>::type::*Func)()>
bool Base<T>::Foo()
{
}

Though it would be easier to implement Foo in Base:

template <typename T>
class Base
    : public T
{
public:
    template <typename W, W& (T::ToImpl::*Func)()>
    bool Foo()
    {
    }
};

Upvotes: 1

Related Questions