On Saturday, 22 October 2016 03:32:44 UTC+3, Mr Flibble wrote:
> On 22/10/2016 00:22, Chris Vine wrote:
> > On Fri, 21 Oct 2016 15:06:44 -0700 (PDT)
> >
woodb...@gmail.com wrote:
> >> On Friday, October 21, 2016 at 1:33:10 AM UTC-5, Öö Tiib wrote:
> >>> Please name at least 3 different cases where you need raw pointer
> >>> and why. Making people to dig in your poorly documented code base
> >>> to try to find it out themselves is fruitless, they don't have time
> >>> for that.
> >>
> >> The documentation isn't very good, but people are free to
> >> take a look a look as you did in a recent thread about std::deque.
> >
> > You haven't answered the question. Please name at least 3 different
> > cases where you need raw pointers and why. (Actually, I would be
> > satisfied by 2, provided they didn't amount to evading the issue, but I
> > cannot speak for your interlocutor).
>
> Just because you are lacking the experience to know where to use a raw
> pointer doesn't mean you can demand that information from someone else.
Why to be so pointlessly patronizing? It smells like Jerry
Stuckle or Rick C. Hodgin and so is good to avoid.
>
> Three use cases for raw pointers immediately come to mind:
> 1) Pointers to array elements (preferable to using indices)
One strong reason why to use raw array is that string literal
is raw array and other is that array dimensions can be deduced from
initializers. Both cases matter about static immutable arrays.
AFAIK the plans about C++17 are to add 'std::array_view' and
'std::string_view' to remove or to reduce those reasons, but I am
not too hopeful about it.
> 2) Non-owning pointer to a polymorphic type the value of which can be
> changed at runtime to point to different objects.
Again it is unclear where we *must* use raw pointer as mutable reference
to non-owned object. What owns it and how? We likely can use raw pointer
but usually other things (like 'std::reference_wrapper', 'std::weak_ptr'
or some iterator) are more suitable.
> 3) Opaque raw pointer used as a handle (type erasure or interfacing with
> C API)
Both the 'std::shared_ptr' and 'std::unique_ptr' can be to opaque type
(unlike 'std::auto_ptr'). There just are some constraints.
For example on case of class with unique-to-opaque member there is
little constraint that rule of zero causes compiler errors and so
developer has to use rule of five.
The smart pointers are useful for wrapping opaque pointers from C API:
using unique_file_ptr = std::unique_ptr<std::FILE, int (*)(std::FILE*)>;
static
unique_file_ptr make_file(const char* filename, const char* flags)
{
return unique_file_ptr(std::fopen(filename, flags), std::fclose);
}
Again if we need a *shared* file pointer then there is little constraint
that we need to make it so that shared_ptr does not call fclose to null
pointer, unique_ptr is smart enough on its own so above works.
>
> I am sure there are plenty of others.
There may be few. For example we need raw pointer as covariant return
type (on the rare case when we need it).
However I wanted to hear some from WoodBrian because he claimed that his
code base is full of such usages where raw pointer is best. Like Chris
pointed out he failed to name any.