On Apr 26, 8:52 pm, W Marsh <wayne.ma...@gmail.com> wrote:
Could anybody tell me wh theparameter"T val" is not marked
constin this Stroustrup code, considering that val is not
modified and not non-constmethods called?
template<class C, class T> int count(constC&v, T val)
{
typename C::const_iterator i = find(v.begin(), v.end(), val);
int n = 0;
while (i != v.end()) {
++n;
++i; // skip past the element we just found
i = find(i, v.end(), val);
}
return n;
}
Because there's no real point in it? Theconstwould be ignored
at the interface level (in the function declaration). One can
argue both ways in the function definition, but in practice,
very few if any programmers useconsthere.
The argument for theconstis that you'll get an error if you
accidentally modify the variable. The argument against is: who
cares? It's your variable, and you should be able to do what
you want with it. There's also the argument that you don't want
the meaninglessconstin the declaration, and you want the
declaration and the definition to be coherent. (This argument
probably applies less to templates, since you often won't have a
separate declaration.)
...