2

I'm a little confused as to what is required regarding calling templated functions in C++11, where default values are supplied for the template arguments.

For example, say I have the following templated function

template <class T = double> void foo()
{
  /* Do something */
}

and I want to call it somewhere in my code. Are all of these valid calls?

foo(); //Is this OK?
foo<>();
foo<int>();

It compiles fine with GCC 4.7.2, but I'm not sure if it's strictly correct C++. I guess my uncertainty may come from the requirement to use empty angle brackets <> when creating an instance of a templated class using the default template, as described here:

Template default arguments

but I presume in my case having the function brackets () is the critical difference?

Community
  • 1
  • 1
Wheels2050
  • 879
  • 2
  • 9
  • 17
  • `foo()` is the same as `foo()`. You added the default template parameter yourself, right? – Ed S. Jun 27 '14 at 03:05
  • I know that they are the same, I'm really just wondering why the <> isn't required here but is for a templated class (see my comment on Cubbi's answer below). – Wheels2050 Jun 27 '14 at 04:22

1 Answers1

10

A function call to a function template where every template parameter is deducible or has a default can use empty angle brackets or can omit them, both ways are valid.

The difference is that if you omit the angle brackets, you're inviting overload resolution to also consider non-template functions that happen to be named foo and happen to be callable with the same (empty in this case) parameter list.

This hasn't changed in C++11.

Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • Thanks for the reply. What is the difference between this situation, then, and the situation that I linked to regarding a new instance of a templated class. Is there an underlying reason why the angle brackets are required in that case and not here, or is it just the way it was defined to work? – Wheels2050 Jun 27 '14 at 04:20
  • @Wheels2050 one is class template, the other is a function template. They are just different entities. – Cubbi Jun 27 '14 at 04:49
  • Right, so it's not some fundamental thing, it's just the way each one is defined to work? – Wheels2050 Jun 27 '14 at 04:51