带有get函数的模板类始终返回参考

Template class with get function ALWAYS returning reference,

本文关键字:返回 参考 get 函数 带有      更新时间:2023-10-16

如何在此处拥有一个名为

的模板类

FrontbackBuffer 带有模板参数 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
};

我想实现以下内容:

  • 要在代码中保持一致,无论缓冲区内部的类型是什么,都可以使用一个函数 getfront/back ,它应该始终返回引用到缓冲区(const或非const取决于类型:例如const int = t应该返回const int&amp;参考!我想像这样编写代码

    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* ....
    

我会喜欢这个

  • 是一种可以接受所有可能类型的缓冲类别(例如共享_ptr)ASD

  • 我想要的只是一些访问权限,它应该是非常性能的,没有副本等等

  • 我不知道该如何编写这个通用缓冲区?有人有任何线索吗?

谢谢!!!

edit1 我也希望能够分配给授权指针:

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

那就是我对特征的问题出现的地方!

您需要专业化模板的一部分(特征技术),如这样:

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

更新
请注意返回参考的特殊功能 - 如果您想对指针进行不同的行为,则需要返回引用。
结束更新

并为参考和const参考进行专业化:

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

((但是,我不确定指针的专业化是一个很好的设计...)


结束更新

和类模板中的用法:

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

它可以按预期工作:http://ideone.com/e7xfon