From: Thiago A. on

It Would be better if unique_ptr were a type modifier?

C++ has many type modifiers line mutable, const, volatile.

In my code I use very few of mutable and even less of volatile
modifier.
In the other hand, the concept of unique pointer I use all the time.

positive points:

- A clear language concept instead of library
- no includes
- Some help to compiler, to generate fast code. (I'm not sure about
that)
- Better error message generated by compiler

I guess that the shared_ptr is also a candidate for a type modifier
but with much more details and implications.

What do you think?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Dragan Milenkovic on
On 07/24/2010 03:55 PM, Thiago A. wrote:
>
> It Would be better if unique_ptr were a type modifier?
>
> C++ has many type modifiers line mutable, const, volatile.
>
> In my code I use very few of mutable and even less of volatile
> modifier.
> In the other hand, the concept of unique pointer I use all the time.
>
> positive points:
>
> - A clear language concept instead of library
> - no includes
> - Some help to compiler, to generate fast code. (I'm not sure about
> that)
> - Better error message generated by compiler
>
> I guess that the shared_ptr is also a candidate for a type modifier
> but with much more details and implications.
>
> What do you think?

I didn't give thought or searched for existing discussions
on this topic, but there is another, much more important item
that you forgot... The support for covariant return types...

--
Dragan

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On Jul 24, 2:55 pm, "Thiago A." <thiago.ad...(a)gmail.com> wrote:
> It Would be better if unique_ptr were a type modifier?
> [...]
>
> What do you think?

You failed to mention what the modifier would do.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Thiago A. on
> > It Would be better if unique_ptr were a type modifier?
>
> > What do you think?
>
> You failed to mention what the modifier would do.



The motivation is very simple.
The idea is to provide information to the compiler to ensure in
compile time the RAII idiom.

The "unique" modifier creates a type that:

- Has the default ctor. Initialize to null.

- Has a destructor that does nothing.
The destructor is public/valid only if the compiler can check the
the resource has been released.

- The assigment operator is public/valid only if the compiler can
check that the pointer is null before the assignment.


I guess..

- operator new / delete and new[] / delete[] defined for unique
pointers

- operator delete makes the pointer equals zero.

- operator delete and delete [] accepts a "deleter" : delete (&Close)
handle;
This will make the compiler understand that the resource has been
released independent of the resource type.

- The type can be used in template specializations sample :
vector<unique int*>


Samples:
(let's say that "unique" is a modifier)

unique int* f1()
{
unique int * p = new unique int();
return p; //ok
}

unique int* f2()
{
unique int * p = new unique int();
delete p; //ok
}


void f3()
{
unique int * p; //null by default
} // error not deleted/moved


void f4()
{
unique int* p1 = new unique int();
unique int* p2 = new unique int();

p1 = p2 ;// error: p1 was not deleted

delete p1;
p1 = std::move(p2); // ok

delete p1;
p1 = new unique int(); //ok

delete p1; //ok
delete p2; //ok
}


class X
{
unique int* p;
~X()
{
}//error: p not deleted/moved

//~X()
//{
// delete p;
//}//ok
};


class X
{
std::vector<unique int*> v;
~X()
{
}//ok
};

void f()
{
unique Handle file;
} //error: missing delete for file


void f()
{
unique Handle file;
delete (&Close) file;
} //ok

template<class T>
vector<unique T*>
{
..
};

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 26 Jul., 16:07, "Thiago A." <thiago.ad...(a)gmail.com> wrote:
> > > It Would be better if unique_ptr were a type modifier?
>
> > > What do you think?
>
> > You failed to mention what the modifier would do.
>
> The motivation is very simple.
> The idea is to provide information to the compiler to ensure in
> compile time the RAII idiom.
>
> The "unique" modifier creates a type that:
>
> - Has the default ctor. Initialize to null.
>
> - Has a destructor that does nothing.
> The destructor is public/valid only if the compiler can check the
> the resource has been released.
>
> - The assigment operator is public/valid only if the compiler can
> check that the pointer is null before the assignment.
>
> I guess..
>
> - operator new / delete and new[] / delete[] defined for unique
> pointers
>
> - operator delete makes the pointer equals zero.
>
> - operator delete and delete [] accepts a "deleter" : delete (&Close)
> handle;
> This will make the compiler understand that the resource has been
> released independent of the resource type.
>
> - The type can be used in template specializations sample :
> vector<unique int*>
>
> Samples:
> (let's say that "unique" is a modifier)

I fail to see the very advantage of a new key word
(with all it's implications - can I/need I specialize
templates for it?) compared to the current library
solution. Or shorter: What is the advantage of

unique* T

over

std::unique_ptr<T>

Could you enlighten us?

Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]