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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:51:03+00:00 2026-06-14T00:51:03+00:00

How is it possible to have a templated class here called FrontBackBuffer with template

  • 0

How is it possible to have a templated class here called

FrontBackBuffer with template parameter TBackBufferType, TFrontBufferType

template< typename TBufferTypeFront, typename TBufferTypeBack = TBufferTypeFront>
class FrontBackBuffer{
  public:
  explicit FrontBackBuffer(
     TBufferTypeFront const & m_front,
     TBufferTypeBack  const & m_back):
     m_Front(m_front),
     m_Back(m_back)
  {
  };

  ~FrontBackBuffer()
  {};

  typename std::remove_reference<
    typename std::remove_pointer<TBufferTypeFront>::type
  >::type  & getFront(){return m_Front;}    // error: invalid initialization of reference of type 'A&' from expression of type 'A*'| (here T is A)


  typename std::remove_reference<
    typename std::remove_pointer<TBufferTypeBack>::type 
  >::type  & getBack(){return m_Back;}

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

};

I would like to achieve the following:

  • to be consistent in the code, I would prefere, to no matter what the Type inside the buffer is, to have a function getFront/Back which should always return a Reference to the buffer (either a const or a non-const depending on the type: e.g const int = T should return a const int & reference! I would like to write code like this

    FrontBuffer<const int&, std::vector<int> > a;
    a.getFront() = 4 //COmpile error! OK!;
    a.getBack()[0] = 4;
    FrontBuffer< int*, GAGAType * > b;
    b.getBack() = GAGAType();
    b.getFront() = int(4);  // this is no ERROR, i would like to get the reference of the memory location pointet by int* ....
    

I would like this because I want to avoid changing the syntax if I change the buffer type from reference to pointer (where I need to dereference)

  • Is such a Buffer class possible to accept with all possible types (like shared_ptr)
    asd

  • All I want is some access and it should be very performant, no copies and so on

  • I dont know really how to write this generic buffer? Somebody has any clue?

Thanks!!!

EDIT1 I want also to be able to assign to the dereferenced pointer:

b.getFront() = int(4);  // this is no ERROR, i would like to get the reference of the memory location pointet by int* ....

Thats where my problem with traits comes in!

  • 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-14T00:51:04+00:00Added an answer on June 14, 2026 at 12:51 am

    You need to specialize (traits technique) part of your template, like this:

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

    Update
    Note the special function for returning reference – it is needed if you want to behave differently for pointers – returns references for it.
    End Update

    And make specialization for references and const references:

    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;
            }
        };
    

    Update
    And this “special” specialization for pointers – so they will work as references:

    //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;
            }
        };
    

    ((However I am not sure this specialization for pointers is a good design… ))

    End Update

    And usage inside your class template:

    template< typename TBufferTypeFront, typename TBufferTypeBack = TBufferTypeFront>
    class FrontBackBuffer{
      public:
    
    
       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 m_front,
         TBufferTypeBackCon m_back):
         m_Front(m_front),
         m_Back(m_back)
      {
      };
    
      ~FrontBackBuffer()
      {};
      // See here special functions from traits are used:
      TBufferTypeFrontRef getFront(){return MyRefTypes<TBufferTypeFront>::getRef(m_Front); }    
      TBufferTypeBackRef getBack(){return MyRefTypes<TBufferTypeBack>::getRef(m_Back); }
    
      TBufferTypeFront m_Front;       ///< The front buffer
      TBufferTypeBack m_Back;         ///< The back buffer
    
    };
    

    It works as expected:
    http://ideone.com/e7xfoN

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

Sidebar

Related Questions

Possible Duplicate: C++ Inherited template classes don't have access to the base class I'm
Possible Duplicate: Where and why do I have to put the “template” and “typename”
template<typename T1, typename T2, typename T3> class A: public A<T1, T2, void> { public:
Possible Duplicate: Convert std::tuple to std::array C++11 Suppose you have: template<class T,int N> struct
Possible Duplicate: C++ invoke explicit template constructor First imagine I have a class Data
I have a templated class with an templated member function template<class T> class A
Possible Duplicate: Where and why do I have to put the “template” and “typename”
I have a templated class template<class U, class V, class W> class S {
I have a template class defined in a header file like this. Here I
Is it possible to have optional template parameter in C++ , for example template

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.