On page 340 of the C++ Programming Language: Special Edition, Stroustrup writes…
The semantic checking of a default argument for a template parameter is done if and (only) when that default argument is actually used. In particular, as long as we refrain from using the default template argument Cmp<T> we can compare() strings of a type for which Cmp<X> wouldn't compile (say, because < wasn't defined for an X). This point is crucial in the design of the standard containers, which rely on a template argument to specify default values.
I’m having trouble wrapping my head around the usage of this. Why would this rule allow strings of type X to be compared, when normally it wouldn’t compile? Wouldn’t this behavior be undesirable?
The given example is:
The idea is that the class template
Cmpmight not be defined or illegal for someT. In that case, you can pass a custom comparison class template:If you do that,
Cmpisn’t used and won’t be checked if it actually would compile.