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!
You need to specialize (traits technique) part of your template, like this:
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:
Update
And this “special” specialization for pointers – so they will work as references:
((However I am not sure this specialization for pointers is a good design… ))
End Update
And usage inside your class template:
It works as expected:
http://ideone.com/e7xfoN