tags:

views:

47

answers:

2

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?

+2  A: 

The given example is:

template<class T, class C = Cmp<T> >
int compare(const String<T>& str1, const String<T>& str2) 
{
    // ... compare using C
}

The idea is that the class template Cmp might not be defined or illegal for some T. In that case, you can pass a custom comparison class template:

compare<char, MyComparer>(str1, str2);

If you do that, Cmp isn't used and won't be checked if it actually would compile.

Georg Fritzsche
A: 

It's saying that (for example) if a parameter defaults to X, but you specify T, then the semantics of X are not checked. This is important because X may not compile (as it says, because some operator may not be defined on X).

e.g.

template <class T = int>
struct foo
{
  static int bar(T x) { return x.size(); }
}

int main()
{
  std::cout << foo<std::string>::bar("hello") << std::endl;
}

This doesn't cause an error even though .size() isn't defined for int. It doesn't cause an error because the default parameter of int isn't actually used.

Answering your question. It's not saying that it would allow X's to be compared, it's saying that if you use T then it doesn't matter if X's can be compared because you're not using X.

Peter Alexander