constancy and template parameters in D2.009+

guslay guslay at gmail.com
Thu Dec 13 10:40:28 PST 2007


Janice Caron Wrote:

> On 12/13/07, guslay <guslay at gmail.com> wrote:
> > but a.method() and const a.method() are not the same method call!
> 
> No one said it was. You're confusing a function call with a template
> instantiation. Be careful - that's a very different thing.

I was not clear. What I meant was that you often instantiate a template class parameterized by T to use a variable "a" of type T and call methods on it.

> Consider a function such as:
> 
>     T max(T)(T x, T y) { return (x > y) ? x : y; }
> 
> Do you /really/ need separate instantiations for T==int and
> T==const(int)? Of course not. Walter's choice is reasonable. (It may
> not be perfect, but it's reasonable).

Well if T is a reference to a const object, yes, I want to keep the const....

But I understand what you mean. Sometimes, constness doesn't matter, for instance if I want allocator(T), I'm only interrested in the sizeof T. I think it's a nice innovative feature. However, I'll argue that this optimization is the exception rather than the rule.

> > This is apart from the side effect that you lose const protection.
> No you don't. If const correctness is ever violated, it's still a compile error.

Good to know!





More information about the Digitalmars-d mailing list