Topic: How about an 'unconst' modifier? (What const is/was for ...)


Author: "Anders J. Munch" <andersjm@dancontrol.dk>
Date: 1999/09/27
Raw View
Andrew Koenig wrote in message ...
>Let's change the example very slightly:
>
> template<class T> class X {
>
> void f() {
> T* p = new T;
> delete p;
> }
> };
>
>Now, f did not choose to make a const pointer.  Nevertheless, inside
>an instantiation of X<const int>, f will be making a const pointer
>even though it did not choose to do so.

Maybe C++ should have an "unconst" modifier with semantics reverse of
const.

template<class T> class X {
void f() {
    unconst T* p = new unconst T;
    delete p;
}
};

Now, no problem with T==const int, since unconst T is just int.  (No
problem with T==int either, since unconst is a no-op with something
already non-const, just like const is a no-op with something already
const.)

There never was a problem until the code was wrapped in a template.
This is a templates problem that has to do with figuring out the right
type to use based on a template parameter.  For figuring-out-types
problems, add type calculation operators; don't change delete-const
rules for this.

- Anders
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]