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.

3
  • 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? Commented Mar 16, 2013 at 9:31
  • Sorry, I thought it was my mistake; I didn't know that it was totally wrong. Voting for close... Commented Mar 16, 2013 at 9: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: liveworkspace.org/code/ZaW3K$2 Commented Mar 16, 2013 at 9:48

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.