Why can't the compiler deduce the template type from default arguments?
I was surprised the following code resulted in a could not deduce template argument for T error: struct foo { template <typename T> void bar(int a, T b = 0.0f) { } }; int main() { foo a; a.bar(5); return 0; } Calling a.bar<float>(5) fixes the issue. Why can't the compiler deduce the type from the default argument? In C++03, the specification explicitly prohibits the default argument from being used to deduce a template argument (C++03 §14.8.2/17): A template type-parameter cannot be deduced from the type of a function default argument. In C++11, you can provide a default template argument for