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 6589313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:09:28+00:00 2026-05-25T17:09:28+00:00

Edit: Welp, I guess this was a terrible idea. Is it possible to make

  • 0

Edit: Welp, I guess this was a terrible idea.

Is it possible to make a smart reference in C++ (for a specific class, since you can’t overload the . operator) with the same semantics as a normal C++ reference, but which is reseated when used in an STL container?

For example, if I have some int_ref class with the normal integer operators overloaded, and construction and assignment looks like this:

class int_ref{
    int * p;
public:
    int_ref(int * ip) : p(ip) {}
    int_ref(const int_ref & other) : p(other.p) { 
        /* maybe some refcounting stuff here */ 
    }
    int_ref & operator = (const int_ref & other){
        if (!p) 
            throw something_bad();
        *p = *other.p;
        return *this;
    }
    void reseat(const int_ref & other){
        p = other.p;
    }
}

Then I can’t use this with std::vector since it won’t reseat the references, and I don’t want this kind of thing:

std::vector<int_ref> vec;
int_ref five = new int(5);
vec.push_back(five);
vec.push_back(new int(1));
std::sort(vec.begin(), vec.end()); // the value of five is now 1

I can use rvalue references to make it play nice with the STL,

int_ref & operator=(int_ref && other){
    reseat(other);
    return *this;
}

But then a function that returns an int_ref would use the rvalue overload, and I’d get this:

int_ref make_number(){
    return new int(10);
}

int_ref ref = new int(5);
int_ref other = ref;
other = make_number();    // instead of copying, as a reference would,
                          // other now points to something other than ref

Is there a way around this? Is this just a terrible idea in general?

  • 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-05-25T17:09:28+00:00Added an answer on May 25, 2026 at 5:09 pm

    One problem with even trying to do this is operator&. For a reference, it gives you the address of the referand (since references have no address). For a element of a container, though, it’s expected to give you the address of the element (since those do have addresses).

    So, an element of a container cannot mimic reference semantics in this respect. If you overload operator& to return the address of the referand, then for example the contiguous storage guarantee of vector is violated, since it says &v[n] == &v[0] + n for all 0 <= n < v.size()

    boost::addressof() was invented to work around this problem, so that you don’t have to use & to get the address of an object in generic code. But even the standard is too lazy to say static_cast<T*>(&static_cast<char&>(v[n])) rather than &v[n]. Even when you’re thinking of using it, it’s quite difficult to decide when you want the actual address of the object, and when you want the address that the author of the object thinks you want. It’s best just never to overload unary operator&. That means you’ll get a partial version of reference semantics, which potentially is confusing in its own way.

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

Sidebar

Related Questions

Edit: Seems numerous people think this is a dumb idea, so I would appreciate
Edit: I posted the whole class (striped a few for the error irrelevant things
EDIT: It looks like I'm completely misinformed. Please close this thread. Gah. For the
Edit: This is currenty kinda resolved, Instead of struggling with the problem, I started
Edit: The below question was answered by this . I have a new updated
Edit: I'm looking for solution for this question now also with other programming languages.
EDIT Leaving this for posterity, but nearly a year later, to get down voted,
EDIT After staring at this for 2 days, I do see one issue. I
EDIT : I've gotten the famous question badge with this question, so I figured
EDIT: it is necessary for this to be a series of 1d Arrays. 2d

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.