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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:54:47+00:00 2026-06-14T01:54:47+00:00

I came across this problem this morning: I want to build a generic class

  • 0

I came across this problem this morning:

I want to build a generic class FrontBackBuffer which I can use as the following (some examples).

EDIT Removed some confusing part!

int bb=10;
int fb=3;
FrontBackBuffer< const int*, int & > buf(&fb, &bb);
buf.getBack() = 4; // change from 10 to 4
// buf.getFront() = 5; NO! is const!

buf.swap();
//NOW getFront() and getBack() should return  4 and 3!

FrontBackBuffer< int, const & int > buf(14, bb);
buf.getBack() = 5; // change from 4 to 5
// buf.getFront() = 5; NO! is const!

buf.swap();   
//NOW getFront() and getBack() should return  5 and 14!

It stores two buffers, should be of the same underlying type (see the static_assert).
These buffers can be of pointer type or reference type and also const or non const.
It has two functions getFront()and getBack(). These functions always return a reference to the underlying buffer, either const or non-const. That is why there are all sorts of MyRefTypes traits spezialization.

The class which works so far is as the following:

template< typename TBufferTypeFront, typename TBufferTypeBack = TBufferTypeFront>
class FrontBackBuffer {

// If <const * int , int&> --> this result in is_same< int , int > 
// STATIC_ASSERT( std::is_same< RemoveModifiers<TBufferTypeFront>::type, typename RemoveModifiers<TBufferTypeFront>::type>::result )

public:

    template <typename T>
    struct MyRefTypes {
        typedef const T & Con;
        typedef T& Ref;
        typedef const T& CRef;
        static Ref getRef(T& v) {
            return v;
        }
    };

//Specialization for Reference
    template <typename T>
    struct MyRefTypes<T&> {
        typedef T & Con;
        typedef T& Ref;
        typedef const T& CRef;
        static inline Ref getRef(T& v) {
            return v;
        }
    };

//Specialization for const Reference
    template <typename T>
    struct MyRefTypes<const T&> {
        typedef const T & Con;
        typedef const T& Ref;
        typedef const T& CRef;
        static inline Ref getRef(const T& v) {
            return v;
        }
    };

//Specialization for const
    template <typename T>
    struct MyRefTypes<const T> {
        typedef const T & Con;
        typedef const T& Ref;
        typedef const T& CRef;
        static inline Ref getRef(const T& v) {
            return v;
        }
    };

//Specialization for pointers
    template <typename T>
    struct MyRefTypes<T*> {
        typedef T* Con;
        typedef T& Ref;
        typedef T* const CRef;  //! note this is a pointer....
        static inline Ref getRef(T* v) {
            return *v;
        }
    };

//Specialization for const pointers
    template <typename T>
    struct MyRefTypes<const T*> {
        typedef const T* Con;
        typedef const T& Ref;
        typedef const T* const CRef; //! note this is a pointer....
        static inline Ref getRef(const T* v) {
            return *v;
        }
    };


    typedef typename MyRefTypes<TBufferTypeFront>::Ref TBufferTypeFrontRef;
    typedef typename MyRefTypes<TBufferTypeFront>::CRef TBufferTypeFrontCRef;
    typedef typename MyRefTypes<TBufferTypeFront>::Con TBufferTypeFrontCon;

    typedef typename MyRefTypes<TBufferTypeBack >::Ref TBufferTypeBackRef;
    typedef typename MyRefTypes<TBufferTypeBack >::CRef TBufferTypeBackCRef;
    typedef typename MyRefTypes<TBufferTypeBack >::Con TBufferTypeBackCon;

    explicit FrontBackBuffer(
        TBufferTypeFrontCon  front,
        TBufferTypeBackCon   back):
        m_Front(front),
        m_Back(back)
    {
             m_pBack = (void*)&m_Back;
             m_pFront = (void*)&m_Front;

    };


    ~FrontBackBuffer()
    {};

    TBufferTypeFrontRef getFront() {
        return MyRefTypes<TBufferTypeFront>::getRef(m_Front);
    }
    TBufferTypeBackRef getBack() {
        return MyRefTypes<TBufferTypeBack>::getRef(m_Back);
    }
    private:

    void swap(){
         void * temp = m_pFront;
         m_pFront = m_pBack;
         m_pBack = temp;
    }

    TBufferTypeFront * m_pFront;       ///< The pointer to front buffer
    TBufferTypeBack * m_pBack;         ///< The pointer to back buffer

    TBufferTypeFront m_Front;       ///< The front buffer
    TBufferTypeBack m_Back;         ///< The back buffer

};

The question is now (which I can not solve completely right):
How can I add a generic function swap() which swaps the buffers, but no copy should be made. I thought about two pointers void * m_pFrontand void *m_pBackwhich I should use to do the job, and assigned correct (see constructor).

But how do I now write these getter functions is a mystery to me:

    TBufferTypeFrontRef getFront() {
        return MyRefTypes<TBufferTypeFront>::getRef(m_Front);
    }
    TBufferTypeBackRef getBack() {
        return MyRefTypes<TBufferTypeBack>::getRef(m_Back);
    }

At the end of the day it should just work 🙂
Thanks for any help and trying to solve this puzzle :-)!

  • 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-14T01:54:49+00:00Added an answer on June 14, 2026 at 1:54 am

    This is the full answer!

    The code works and the buffer is generic as hell 🙂 –>
    http://ideone.com/m3kFmo

    And the swap function is also efficient and works as desired!

    Of course this class is only usefull when the underlying type is the same like:
    FrontBackBuffer< int *, int &> –> OK

    FrontBackBuffer< int *, const double &> –> is ok, but swap function makes no sense then!

    Here the code! 🙂

    template< typename TBufferTypeFront, typename TBufferTypeBack = TBufferTypeFront>
    class FrontBackBuffer {
    
        // If <const * int , int&> --> this result in is_same< int , int > 
        // STATIC_ASSERT( std::is_same< RemoveModifiers<TBufferTypeFront>::type, typename RemoveModifiers<TBufferTypeFront>::type>::result )
    
    public:
    
        template <typename T>
        struct MyRefTypes {
            typedef T Org;
            typedef T* Ptr;
            typedef const T & Con;
            typedef T& Ref;
            typedef const T& CRef;
            static inline Ptr getUnderlyingPtr(T& v) {
                return &v;
            }
            static Ref getRef(T& v) {
                return v;
            }
        };
    
        //Specialization for Reference
        template <typename T>
        struct MyRefTypes<T&> {
            typedef T Org;
            typedef T* Ptr;
            typedef T & Con;
            typedef T& Ref;
            typedef const T& CRef;
            static inline Ptr getUnderlyingPtr(T& v) {
                return &v;
            }
            static inline Ref getRef(T& v) {
                return v;
            }
        };
    
        //Specialization for const Reference
        template <typename T>
        struct MyRefTypes<const T&> {
            typedef T Org;
            typedef T* Ptr;
            typedef const T & Con;
            typedef const T& Ref;
            typedef const T& CRef;
            static inline Ptr getUnderlyingPtr(const T& v) {
                return &const_cast<T&>(v);
            }
            static inline Ref getRef(const T& v) {
                return v;
            }
        };
    
        //Specialization for const
        template <typename T>
        struct MyRefTypes<const T> {
            typedef T Org;
            typedef T* Ptr;
            typedef const T & Con;
            typedef const T& Ref;
            typedef const T& CRef;
            static inline Ptr getUnderlyingPtr(const T& v) {
                return &const_cast<T&>(v);
            }
            static inline Ref getRef(const T& v) {
                return v;
            }
        };
    
        //Specialization for pointers
        template <typename T>
        struct MyRefTypes<T*> {
            typedef T* Ptr;
            typedef T Org;
            typedef T* Con;
            typedef T& Ref;
            typedef T* const CRef;  //! note this is a pointer....
            static inline Ptr getUnderlyingPtr(T* v) {
                return v;
            }
            static inline Ref getRef(T* v) {
                return *v;
            }
        };
    
        //Specialization for const pointers
        template <typename T>
        struct MyRefTypes<const T*> {
            typedef T Org;
            typedef T* Ptr;
            typedef const T* Con;
            typedef const T& Ref;
            typedef const T* const CRef; //! note this is a pointer....
            static inline Ptr getUnderlyingPtr(const T* v) {
                return const_cast<T*>(v);
            }
            static inline Ref getRef(const T* v) {
                return *v;
            }
        };
    
    
        typedef typename MyRefTypes<TBufferTypeFront>::Ref TBufferTypeFrontRef;
        typedef typename MyRefTypes<TBufferTypeFront>::CRef TBufferTypeFrontCRef;
        typedef typename MyRefTypes<TBufferTypeFront>::Con TBufferTypeFrontCon;
        typedef typename MyRefTypes<TBufferTypeFront>::Org TBufferTypeFrontOrg;
        typedef typename MyRefTypes<TBufferTypeFront>::Ptr TBufferTypeFrontPtr;
    
        typedef typename MyRefTypes<TBufferTypeBack >::Ref TBufferTypeBackRef;
        typedef typename MyRefTypes<TBufferTypeBack >::CRef TBufferTypeBackCRef;
        typedef typename MyRefTypes<TBufferTypeBack >::Con TBufferTypeBackCon;
        typedef typename MyRefTypes<TBufferTypeBack >::Org TBufferTypeBackOrg;
        typedef typename MyRefTypes<TBufferTypeBack >::Ptr TBufferTypeBackPtr;
    
        explicit FrontBackBuffer(
                                 TBufferTypeFrontCon  front,
                                 TBufferTypeBackCon   back):
        m_Front(front),
        m_Back(back)
        {
            m_pBack = MyRefTypes<TBufferTypeBack>::getUnderlyingPtr(m_Back);
            m_pFront = MyRefTypes<TBufferTypeFront>::getUnderlyingPtr(m_Front);
    
        };
    
    
        ~FrontBackBuffer()
        {};
    
        TBufferTypeFrontRef getFront() {
            return *m_pFront;
        }
        TBufferTypeBackRef getBack() {
            return *m_pBack;
        }
    
        void swap(){
            TBufferTypeFrontPtr temp = m_pFront;
            m_pFront = m_pBack;
            m_pBack = temp;
        }
    
    private:
    
    
    
        TBufferTypeFrontPtr m_pFront;       ///< The pointer to front buffer
        TBufferTypeBackPtr  m_pBack;         ///< The pointer to back buffer
    
        TBufferTypeFront m_Front;       ///< The front buffer
        TBufferTypeBack m_Back;         ///< The back buffer
    
    };
    

    We are able to use it in this way now:

    int main() {
            int front=10;
        int back=3;
        FrontBackBuffer< const int*, int & > buf1(&front, back);
        buf1.getBack() = 4; // change from 3 to 4
        // buf.getFront() = 5; NO! is const!
        buf1.swap();
            std::cout << buf1.getFront() << buf1.getBack() << std::endl;
        //NOW getBack() and getFront() should return  4 and 10!
    
            front = 1;
            back= -1;
        FrontBackBuffer<int &, int > buf2(front, back);
        buf2.getBack() = 2; 
        buf2.getFront() = 3; 
    
        buf2.swap();   
        //NOW getBack() and getFront() should return  2 and 3!
        std::cout << buf2.getFront() << buf2.getBack() << std::endl;
    
            front = 1;
            back= -1;
        FrontBackBuffer<int, const int &> buf3(front, back);
        //buf3.getBack() = 2; // IS CONST!!
        buf3.getFront() = 3; 
    
        buf3.swap();   
        //NOW getBack() and getFront() should return  -1 and 3!
        std::cout << buf3.getFront() << buf3.getBack() << std::endl;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this problem when I want to check what I input is
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I
I'm starting with node expressjs framework and I came across this problem I can't
I came across this problem in javabat( http://www.javabat.com/prob/p183562 ): We want to make a
I have been thinking of this problem for some time now,I came across this
I was testing out covariant return types and came across this problem. class Vehicle
I came across this problem when attempting to learn python. Consider the following function:
I have came across this problem a few times and can't seem to figure
I came across this problem, I have a number of type long which represents
I came across this problem during an interview forum., Given an int array which

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.