Topic: no_destroy (Was: delete of const objects o.k.?)
Author: rado42 <rado42@my-deja.com>
Date: 1999/10/07 Raw View
>
> To satisfies this need I invent and describe a new
> keyword: no_destroy.
>
It looks like the destroyability of const obbjects will
not change, so I think that instead of discussing it,
its better to find an usable "non-destruct" wrapper.
E.G.
template <class Base>
class Undeletable_T
{
Base* ref;
public:
Undeletable_T (Base *from)
: ref (from)
{}
Base* operator->()
{
return ref;
}
Base& operator *()
{
return *ref;
}
};
(Can access pointer in 'reasanable way' only).
Usage:
class A
{
public:
int a;
};
void func (Undeletable_T<A> par) // 'Acts' as pointer to 'A'
{
std::cout << par->a;
par->a = 0;
*par = A();
delete par; // compile error here
}
A* a;
func (a); //
delete a;
--
rado
http://members.tripod.com/~radosoft
Sent via Deja.com http://www.deja.com/
Before you buy.
[ 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 ]
Author: Gabriel Dos_Reis <gdosreis@korrigan.inria.fr>
Date: 1999/10/04 Raw View
sbnaran@uiuc.edu (Siemel B. Naran) writes:
[...]
| Another alternative is to invent a new class static_ptr. This
| alternative is better because it doesn't mess around with the
| core language.
|
| template <class T>
| class nodelete_pointer {
| public:
| nodelete_pointer(T * ptr) : ptr(ptr) { }
| T * operator->() const { return ptr; }
| T& operator*() const { return *ptr; }
| private:
| T * ptr;
| };
Do you mean trustful_pointer_to_const? It was deemed ugly.
--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr
---
[ 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 ]
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/10/03 Raw View
In the discussion about the deletion of const object,
some people deplored the lack of a way of expressing
that a function doesn't delete an object through a
pointer passed as argument w/o a cast. Some people said
that const should express that, but, even if it did,
we would still be left alone if we want to pass a
non const pointer.
To satisfies this need I invent and describe a new
keyword: no_destroy.
-- cut here --
no_destroy is a keyword.
[Note: it is not possible to destroy an object trough
a pointer to a no_destroy object except with a cast.]
[Note: having a variable of no_destroy T* doesn't
implies that its value will remain valid forever,
just as having a const T* doesn't imply that the
pointed to value won't change.]
no_destroy is a cv-qualifier, and usual cv-qualifiers
rules apply.
[Example:
void foo (const no_destroy int* pndci, const int* pci)
{
pndci = pci; // well-formed
pci = pndci; // ill-formed
}
]
[Note: just as const, no_destroy is useful for
function interfaces, in that it states a
contract between the callee and the caller. ]
The delete expression, and a direct-dtor-call, shall
not be called on a pointer to an object declared no_destroy.
[Example:
struct T {};
void foo (const no_destroy T* pndci, const T* pndi)
{
delete pndci; // ill-formed
pndci->~T(); // ill-formed
delete pndi; // well-formed
pndi->~T(); // well-formed
}
]
[Note: an object of a no_destroy type doesn't
satisfies the requirements of CopyConstructible
(20.1.3), so standards containers cannot be
instantiated with such type. ]
--
Valentin Bonnard
[ 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 ]
Author: sbnaran@uiuc.edu (Siemel B. Naran)
Date: 1999/10/04 Raw View
On 3 Oct 1999 14:51:15 GMT, Valentin Bonnard <Bonnard.V@wanadoo.fr> wrote:
> void foo (const no_destroy int* pndci, const int* pci)
> {
> pndci = pci; // well-formed
> pci = pndci; // ill-formed
> }
Earlier on this thread someone suggested we just use the 'static'
keyword. This is better because it doesn't invent a new keyword.
It is identical to your 'no_destroy' keyword. But pay attention
to the placement of the keyword --
void foo (int int *static pndci, int const * pci) { ... }
Another alternative is to invent a new class static_ptr. This
alternative is better because it doesn't mess around with the
core language.
template <class T>
class nodelete_pointer {
public:
nodelete_pointer(T * ptr) : ptr(ptr) { }
T * operator->() const { return ptr; }
T& operator*() const { return *ptr; }
private:
T * ptr;
};
--
--------------
siemel b naran
--------------
---
[ 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 ]