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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:45:01+00:00 2026-05-15T15:45:01+00:00

I have a function address_of , which returns a Pointer (encapsulating a shared_ptr )

  • 0

I have a function address_of, which returns a Pointer (encapsulating a shared_ptr) to its argument. address_of needs to work on both lvalues and rvalues, so there are two versions of address_of: one accepting a reference and the other accepting an rvalue reference. Since taking the address of a temporary is a Bad Thing™, the rvalue version of address_of needs to perform a move-construct in order for the Pointer to actually own something. The implementation is straightforward:

template<class T>
inline Pointer address_of(T& value) {
    return Pointer(&value);
}

template<class T>
inline Pointer address_of(T&& value) {
    return Pointer(new T(std::move(value)));
}

And taking the address of a temporary works as expected:

Pointer p = address_of(Derived());

But when I test with the following code:

Base* object = new Derived();
Pointer p = address_of(*object);

GCC complains that the call to address_of is ambiguous:

error: call of overloaded ‘address_of(Base&)’ is ambiguous
note: candidates are: Pointer address_of(T&) [with T = Base]
note:                 Pointer address_of(T&&) [with T = Base&]

I was under the impression that unary * always returns an lvalue, in which case the rvalue version shouldn’t even be considered. What exactly is going on here?

  • 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-15T15:45:01+00:00Added an answer on May 15, 2026 at 3:45 pm

    The problem is caused by reference decay: (correct term is “reference collapse”)

    template < typename T >
    void f(T && t) { .... }
    
    int x; f(x); // what is f()?
    

    The answer to the question in the code is that f() is:

    void f(T& && t) { .... }
    

    Which because of reference decay turns into this:

    void f(T& t) { .... }
    

    As you can expect, this will of course be ambiguous with anything defined as:

    template < typename T >
    void f(T & t) { .... }
    

    This might work (fixed version):

    #include <type_traits>
    #include <utility>
    
    template < typename T >
    struct pointer
    {
      pointer(T& t) {}
      pointer(T&& t) {}
    };
    
    template < typename T >
    pointer<typename std::remove_reference<T>::type> 
    address_of(T && t)
    { 
      return pointer<typename std::remove_reference<T>::type>(std::forward<T>(t));
    }
    
    int main()
    {
      int x = 5;
      pointer<int> o = address_of(x);
      pointer<int> p = address_of(5);
    }
    

    The reason being that this reference decay stuff only happens in functions that are templated on T. In this case your pointer class is, but the constructors are not actually templates themselves and so T& is never a valid T for the T&& version.

    The first version still had the same problem as your code since address_of was just using T as the template parameter for pointer. We actually need the raw type.

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

Sidebar

Ask A Question

Stats

  • Questions 447k
  • Answers 448k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Update becuase the first example would not work. You could… May 15, 2026 at 7:43 pm
  • Editorial Team
    Editorial Team added an answer use inline keyword template<> inline void Alpha::foo<int>() {} alternatively, provide… May 15, 2026 at 7:43 pm
  • Editorial Team
    Editorial Team added an answer Thanks to all who had taken time to think on… May 15, 2026 at 7:43 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.