1

I have the following, rather simple, template:

template< typename T >
struct compare {
  bool operator()( T a, T b ) { return a < b; }
};

template< typename T, typename Comp = compare< T > >
T min( T a, T b ) {                                  // line 22
  Comp comp;
  if( comp( a, b ) ) return a;
  return b;
}

template< typename T, typename Comp = compare< T > >
T max( T a, T b ) {                                  // line 29
  Comp comp;
  if( !comp( a, b ) ) return a;
  return b;
}

When I compile the previous code I get the following errors:

utility.h:22: error: default template arguments may not be used in function templates
utility.h:29: error: default template arguments may not be used in function templates

I have written before many templates like this, much more complicated, but it's the first time I get this error. What am I doing wrong? Thanks in advance.

Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58
  • 3
    The error message seems pretty clear to me - default arguments are not allowed for template *functions* (but are allowed for template *classes*). Is there something specific that you are not understanding? Or are you asking why this isn't allowed? – Nik Bougalis Mar 16 '13 at 09:31
  • Sorry, I thought it was my mistake; I didn't know that it was totally wrong. Voting for close... – Rontogiannis Aristofanis Mar 16 '13 at 09:44
  • @Rondogiannis It isn't wrong anymore in C++11. Get an up-to-date compiler, compile in C++11 mode and you can enjoy function templates with default arguments: http://liveworkspace.org/code/ZaW3K$2 – us2012 Mar 16 '13 at 09:48

0 Answers0