Re: What is the correct template type?
On Sunday, December 28, 2014 1:57:13 AM UTC+2, JiiPee wrote:
On 27/12/2014 23:34, =D6=F6 Tiib wrote:
No compiler nearby but it might be that this works like you want:
template <typename T> class pair2_derived: public pair2<T, T> {
public: typedef pair2<T, T> base; T& xx = base::first; }; int main() =
{
pair2_derived<int> x; x.first = 42; x.second = 43; std::cout << "xx=
is:" << x.xx << std::endl; return 0; }
I need this because I am trying to use std::pair as a base for 2dvector=
and 2dsize. Just testing how it would go... so I have to put
first/second to be refered by x/y and height/width.
So the goal is renaming/aliases? Other question is why you need pair there?
It does not have much of functionality. Just relational operators and
'swap'.
The references are bad for renaming/aliasing because these take some memory=
..
Your derived objects will be bigger. It is better to use short accessory
functions for such aliases. Sort of like:
struct Vertex
{
float first() const { return values[0]; }
float second() const { return values[1]; }
float third() const { return values[2]; }
float x() const { return values[0]; }
float y() const { return values[1]; }
float z() const { return values[2]; }
float operator [] (int i) const { return values[i]; }
float& operator [] (int i) { return values[i]; }
float[3] values;
};
The short member function calls will be usually optimized out by
compiler.