1

怎么可能有一个模板类在这里称为

带有模板参数TBackBufferType 、TFrontBufferType 的 FrontBackBuffer

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 或一个非常量,具体取决于type: eg const int = T 应该返回一个 const int & 引用!我想写这样的代码

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

我想要这个,因为如果我将缓冲区类型从引用更改为指针(我需要取消引用),我想避免更改语法

  • 这样的 Buffer 类是否可以接受所有可能的类型(如 shared_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* ....

这就是我的特征问题所在!

4

1 回答 1

5

您需要专门化(特征技术)模板的一部分,如下所示:

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

更新
请注意返回引用的特殊函数 - 如果您想对指针采取不同的行为,则需要它 - 为它返回引用。
结束更新

并对引用和常量引用进行专门化:

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

于 2012-11-08T15:50:29.570 回答