Good question. 3 people have already answered to say it "doesn't make sense", and they're all wrong in general. Function template parameters are not always deductible from the function call parameters. For example, if they were allowed I could write template <int N = 1> int &increment(int &i) { i += N; return i; }, and then increment(i); or increment<2>(i);. As it is, I have to write increment<1>(i);.
– Steve JessopMar 15 '10 at 13:48

Actually, mine and AraK's examples can both be dealt with by overloading. litb's can't, I think, because the template parameter might be deduced or might be specified.
– Steve JessopMar 15 '10 at 13:57

3

@Steve: The missing semicolon is actually a new EOL operator overload to complement B. Stavtrup's "Overloading of C++ Whitespace" published in Journal of Object-Oriented Programming, April 1, 1992. (www2.research.att.com/~bs/papers.html)
– Roger PateMar 15 '10 at 14:43

The prohibition of default template arguments for function templates is a misbegotten remnant of the time where freestanding functions were treated as second class citizens and required all template arguments to be deduced from the function arguments rather than specified.

The restriction seriously cramps programming style by unnecessarily making freestanding functions different from member functions, thus making it harder to write STL-style code.

@Arman, the defect report link contains the changes that are made to the working draft for C++0x and the discussions. Arguments neither deduced nor explicitly specified are obtained from default arguments. GCC4.4 supports default arguments for function templates in C++0x mode.
– Johannes Schaub - litbMar 15 '10 at 13:59

and the obligatory follow up question... when is this expected to make it's way into other compilers :)
– Jamie CookDec 10 '10 at 5:13

@JohannesSchaub-litb I had the same problem: no possibility to speficy the default type in a template function. I have solved by an explicit instantiation of the function on the default type (double in my case). Perhaps it is not "general", but is there any drawback with this practice? Thanks.
– JackOLanternMay 5 '13 at 18:33

When templates were originally added to the C++ language, explicit function template arguments were not a valid construct. Function template arguments always had to be deducible from the call expression. As a result, there seemed to be no compelling reason to allow default function template arguments because the default would always be overridden by the deduced value.

The printed output matches the comments for each call to f, and the commented-out call fails to compile as expected.

So I suspect that default template parameters "aren't needed", but probably only in the same sense that default function arguments "aren't needed". As Stroustrup's defect report indicates, the addition of non-deduced parameters was too late for anyone to realise and/or really appreciate that it made defaults useful. So the current situation is in effect based on a version of function templates which was never standard.

Probably just one of those things. The C++ standardisation process runs slow in part so that people have time to realise when a change creates opportunities or difficulties elsewhere in the standard. Difficulties hopefully are caught by the people implementing the draft standard as they go along, when they spot a contradiction or ambiguity. Opportunities to allow things that weren't allowed before, rely on someone who wants to write the code noticing that it no longer needs to be illegal...
– Steve JessopMar 15 '10 at 15:22

2

One more for you: template<typename T = void> int SomeFunction();. The template parameter here is never used, and in fact the function is never called; the only place it is referred to is in a decltype or sizeof. The name deliberately matches the name of another function but the fact it's a template means the compiler will prefer the free function if it exists. The two are used in SFINAE to provide default behaviour where a function definition is missing.
– TomAug 7 '12 at 14:05

Note that, while this does disable the "default template arguments are only allowed on a class template" message, it doesn't actually make the template instantiation process use the provided value. That requires VS2013 (or any other compiler that has completed C++11 defect 226 "Default template arguments for function templates")
– puetzkJan 19 '15 at 5:11

This is a workaround which is pretty neat but it won't cover all the cases you might want. For example how would you apply this to a constructor?
– Tiberiu SavinAug 20 '13 at 1:00

@TiberiuSavin - if I understood you correctly, then you can do like this: template <typename E, typename ARR_E> worker<E, ARR_E>::worker(ARR_E* parr) { parr_ = parr; }. And then use it like this: worker<int> w2(&my_array);
– alariqNov 14 '13 at 18:11