带有get函数的模板类始终返回参考
Template class with get function ALWAYS returning reference,
如何在此处拥有一个名为
的模板类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
相关文章:
- 赋值运算符的返回值可以作为参考吗?
- C 标准:通过复制返回以初始化无RVO的参考:是否有任何副本
- JNI返回Java对象,可以返回本地参考,还是必须是全局
- 是否可以通过引用通过参数返回参考
- 返回int数据类型v/s传递参考
- 通过C 中的参考返回结果
- 在C 中返回参考有效期参考是否有效
- 通过参考返回C 中的对象
- 为什么const rvalue合格的std ::可选:: value()返回const rvalue参考
- 转发参考:给定 T&& 时返回 T&& 和给定 T&&时返回 T&
- C 通过参考返回本地对象
- C 按值返回与参考返回
- 参考未返回
- 分配运算符过载:返回void与返回参考参数
- 分配给返回参考的函数
- 使用Ternery表达式返回参考会导致运行时错误
- 带有get函数的模板类始终返回参考
- 数组[]运算符返回参考,因此如何更改其值
- 返回参考时返回错误
- 返回参考资料在这里是如何工作的