Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:26:05+00:00 2026-06-15T23:26:05+00:00

If I could I would remove all raw pointers * from my code, because

  • 0

If I could I would remove all raw pointers * from my code, because using them may be not thread safe and intentions of the design are not clear (optional value, ownership, etc).
Sometimes however it is not that easy to not use pointers. For example we tend to use pointers for a base type in a container of polymorphic types:

class A : noncopyable { ... };
class B : public A { ... };

std::vector<A*> v;
v.emplace_back(new B);

// temporary container for some operation
std::vector<A*> selected;
if(check())
   selected.emplace_back(v.front());

What can you say about above code? Who is the owner? Is it a shared ownership or not? It is why we should probably do that for v:

std::vector<std::unique_ptr<A>> v;
v.emplace_back(make_unique<B>());

Now it is clear that v owns the objects but I still do not like that selected has a raw pointer and makes my design not intuitive. Looking into Standard C++ library I think that there is only one type that could do the job – std::reference_wrapper:

std::vector<std::unique_ptr<A>> v;
v.emplace_back(make_unique<B>());

// temporary container for some operation
std::vector<std::reference_wrapper<A>> selected;
if(check())
  selected.emplace_back(*v.front());

How do you feel about that code? Is it a good practice? I know that std::ref() and std::cref where meant to primarily work with templates, but it seems that here we can also use it to clearly state our design intent. The only problem I see is that I have to dereference std::reference_wrapper with get() and there are no operator*() or operator->() inside to have the same interface like in a container with unique_ptr. Should I write something similar on my own? Or maybe a reference_wrapper could be extended for such use case in future C++ versions? Please share your feedback.

EDIT: I changed the code samples to maybe better show the intent.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:26:06+00:00Added an answer on June 15, 2026 at 11:26 pm

    You have already provided a solution which looks sound. I understand that the question is “How do you feel?”

    My personal feeling is that there need to exist some balance between safety and unambiguity on the one hand and the simplicity of the code on the other. It looks like your solution may be pushing it too hard towards safety and compromising the simplicity too much. Whenever I used containers holding “weak references” I used raw pointers to represent these. True, this might make it less clear who the owner of the object is, but it has some advantages too: you do not have to study what a “reference_wrapper” is, and the code is clear. If you use them (a container of weak references) only temporarily and you encapsulate this usage, the ownership issue should be minimal.

    But this is just a question of personal preference, I guess. Let me just propose using different types for the same purpose. This is provided that you can afford to use Boost. For “strong” references (which own the resource) you could use Steve Watanabe’s Type Erasure library. It does not require an explicit usage of free-store memory, and i suppose for small types it can get away from using heap-memory altogether (using small-buffer optimization). It has been recently accepted to Boost, although has not been release yet, I think.

    For weak references, consider using “optional references” with Boost.Optional:

    int i = 0;
    boost::optional<int&> oi = i; // note: int&
    i = 2;
    assert(*oi == 2);
    

    It has same semantics as reference_wrapper.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I remove all the values from a dropdown list using jQuery? idnum
I would like to remove all unused CSS selectors from a common CSS file.
I would like to remove all substrings from a string in java that begin
I would like to know how could I use JSON for passing data from
Is there a macro somebody knows of or could write that would check all
I need to remove all occurences of &page=* from a string, where * is
I would like to remove all the labels such as the city names that
I want to remove all rows apart from the row with id 'row0' from
Given a path like /a/./b/c/../d , I would like to remove all the current
I am trying to write a code to remove all the leading, trailing and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.