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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:01:05+00:00 2026-06-16T15:01:05+00:00

I have a class as below class A { public: A(int key) : m_key(key)

  • 0

I have a class as below

class A
{
public:
    A(int key)          : m_key(key) {}
    int Key() const     {return m_key;}

private:
    int m_key;
};

I test using unique_ptr with member function pointer

int (A::*MemFun)() const;
MemFun = &A::Key;
( std::unique_ptr<A>(new A(10))       ->*MemFun ) (); // Error C2296
( std::unique_ptr<A>(new A(10)).get() ->*MemFun ) (); // okay
(*std::unique_ptr<A>(new A(10))        .*MemFun ) (); // okay

The first one gives a compilation error (VC2010 gives error C2296, illegal, left operator includes std::unique_ptr<_Ty>). Why? Thanks.

  • 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-16T15:01:06+00:00Added an answer on June 16, 2026 at 3:01 pm

    It seems the operator->*() operator isn’t overloaded for std::unique_ptr<T>. The reason why this operator isn’t defined isn’t entirely clear although I think that at the time when the smart pointers were proposed the necessary mechanics for dealing with the suitable overloads were not in place.

    The problem is that operator->*() needs to deal with returning the bound result. For a simple member function this is reasonably simple but for functions it isn’t entirely trivial. Here is a minimalistic variation of the unique_ptr<T> class template which just shows the implementation would look like:

    template <typename T>
    struct unique_ptr
    {
        T* p_;
        unique_ptr(T* p): p_(p) {}
        T* operator->() { return this->p_; }
        template <typename R>
        R& operator->*(R T::*mem) { return this->p_->*mem; }
        template <typename R>
        auto operator->*(R (T::*mem)()) ->decltype(std::bind(mem, this->p_))
        {
            return std::bind(mem, this->p_);
        }
    };
    

    This version merely copes with pointers to member variables and pointer to member functions with no arguments. I need to meditate a bit over a version of the operator->*() operator for arbitrary number of arguments. The version for pointer to member variables is trivial: It just needs to return a reference the corresponding member. The version for member functions needs to create a callable object with the first (implicit) parameter being bound to the correct object.

    Dealing with an arbitrary number of arguments take a bit of playing with variadic arguments. A definition of unique_ptr<T> also dealing with member functions pointers taking arguments could look something like this:

    template <typename T>
    struct unique_ptr
    {
    private:
        T* p_;
        template <typename R, typename... A, int... I>
        auto bind_members(R (T::*mem)(A...), indices<I...>)
            -> decltype(std::bind(mem, this->p_, placeholder<I + 1>()...))
        {
            return std::bind(mem, this->p_, placeholder<I + 1>()...);
        }
    
    public:
        unique_ptr(T* p): p_(p) {}
        T* operator->() const { return this->p_; }
        template <typename R>
        R& operator->*(R T::*mem) { return this->p_->*mem; }
        template <typename R>
        auto operator->*(R (T::*mem)()) ->decltype(std::bind(mem, this->p_))
        {
            return std::bind(mem, this->p_);
        }
        template <typename R, typename... A>
        auto operator->*(R (T::*mem)(A...))
            -> decltype(this->bind_members(mem,
                    typename indices<sizeof...(A) - 1>::type())) {
            return this->bind_members(mem,
                typename indices<sizeof...(A) - 1>::type());
        }
    };
    

    The main trick consists in creating a sequence of suitable placeholders for the arguments. The corresponding helper classes are defined thus:

    template <int... Indices> struct indices;
    template <> struct indices<-1> { typedef indices<> type; };
    template <int... Indices>
    struct indices<0, Indices...>
    {
        typedef indices<0, Indices...> type;
    };
    template <int Index, int... Indices>
    struct indices<Index, Indices...>
    {
        typedef typename indices<Index - 1, Index, Indices...>::type type;
    };
    
    template <int I>
    struct placeholder
        : std::integral_constant<int, I>
    {
    };
    
    namespace std
    {
        template <int I>
        struct is_placeholder<placeholder<I>>
            : std::integral_constant<bool, true>
        {
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have the code below : public class Anything { public int Data {
I have the below tables as my entities public class Customer { public int
I have a class that implements InvocationHandler as below: public class MyProxyClass implements InvocationHandler
Say I have a class with three constructors, like the one below: public class
I have a Student class in my application as below: public class Student {
Say I have a simple address class like below: public class Address { public
Does making the below class final (adding public final class) have any real impact
I have create a Java class extending LinearLayout as shown below public class CustomLinear
I have create a Java class extending LinearLayout as shown below public class News

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.