0

For example

template<class T>
T make()
{
   return T();
}

and I want to specialize it when T is a class template A;

template<int N>
class A
{};

template<int N>
A<N> make<A<N>>()
{
   ...
};

Error in compilation: illegal use of explicit template arguments

How to do it?

user1899020
  • 13,167
  • 21
  • 79
  • 154

2 Answers2

1

What you are trying to do is partial specialization and is not allowed. It will be better to wrap that in a struct.

template<class T>
struct Maker
{
   T make() { return T(); }
};

template<int N>
class A
{};

template<int N>
struct Maker<A<N>>
{
   A<N> make()
   {
      return A<N>();
   }
};
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

This is not a partial specialization but an overload. Just remove <A<N>>:

template<int N>
A<N> make()
{
   ...
};
O'Neil
  • 3,790
  • 4
  • 16
  • 30
  • It is a partial specialization. You make a new method make() which returns A other than int. – user1899020 Feb 03 '17 at 04:39
  • @user1899020 You are using a type `` (`make()`) in the first overload. Here you want it with an `int` (`make<0>()`). A partial specialization would concist of a more specialized `T` (e.g. integral type). – O'Neil Feb 03 '17 at 04:47