Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is the correct template type?

24 views
Skip to first unread message

JiiPee

unread,
Dec 27, 2014, 5:36:18 PM12/27/14
to
I am trying to make xx (in pair2_derived) to be a reference member
variable so that it refers to "first" in pair2. What should I put as a
type for xx? I first tried T (because it is/will be the type in real
life), but I get "error: expected ';' at end of member declaration". I
tried other ones as well like pair2<T, T>::_T1 but none worked. I am not
very familiar with templates so would need some help here, thanks.

template<class _T1, class _T2>
struct pair2
{
_T1 first;
_T2 second;
};

template <typename T>
class pair2_derived: public pair2<T, T>
{
public:
T& xx = pair2<T, T>::first; // xx has a wrong type...what should the
type of xx be?
};

---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com

Paavo Helde

unread,
Dec 27, 2014, 6:17:42 PM12/27/14
to
JiiPee <n...@notvalid.com> wrote in news:svGnw.903302$u67.8...@fx08.am4:

> I am trying to make xx (in pair2_derived) to be a reference member
> variable so that it refers to "first" in pair2. What should I put as a
> type for xx? I first tried T (because it is/will be the type in real
> life), but I get "error: expected ';' at end of member declaration". I
> tried other ones as well like pair2<T, T>::_T1 but none worked. I am
not
> very familiar with templates so would need some help here, thanks.
>
> template<class _T1, class _T2>
> struct pair2
> {
> _T1 first;
> _T2 second;
> };

Identifiers like _T1 are reserved for implementations, it is illegal to
use them in your own code.

> template <typename T>
> class pair2_derived: public pair2<T, T>
> {
> public:
> T& xx = pair2<T, T>::first; // xx has a wrong type...what should
the
> type of xx be?
> };

template <typename T>
class pair2_derived: public pair2<T, T>
{
public:
T& xx;
pair2_derived(): xx(this->first) {}
};

JiiPee

unread,
Dec 27, 2014, 6:33:38 PM12/27/14
to
On 27/12/2014 23:17, Paavo Helde wrote:
> JiiPee <n...@notvalid.com> wrote in news:svGnw.903302$u67.8...@fx08.am4:
>
>> I am trying to make xx (in pair2_derived) to be a reference member
>> variable so that it refers to "first" in pair2. What should I put as a
>> type for xx? I first tried T (because it is/will be the type in real
>> life), but I get "error: expected ';' at end of member declaration". I
>> tried other ones as well like pair2<T, T>::_T1 but none worked. I am
> not
>> very familiar with templates so would need some help here, thanks.
>>
>> template<class _T1, class _T2>
>> struct pair2
>> {
>> _T1 first;
>> _T2 second;
>> };
> Identifiers like _T1 are reserved for implementations, it is illegal to
> use them in your own code.
>
>

Ok, but that does not seem to be the problem here. Even if I take _ off
from it for example
pair2<T, T>::T1& xx = pair2<T, T>::first;

does not work.

Öö Tiib

unread,
Dec 27, 2014, 6:34:57 PM12/27/14
to
On Sunday, December 28, 2014 12:36:18 AM UTC+2, JiiPee wrote:
> I am trying to make xx (in pair2_derived) to be a reference member
> variable so that it refers to "first" in pair2. What should I put as a
> type for xx? I first tried T (because it is/will be the type in real
> life), but I get "error: expected ';' at end of member declaration". I
> tried other ones as well like pair2<T, T>::_T1 but none worked. I am not
> very familiar with templates so would need some help here, thanks.
>
> template<class _T1, class _T2>
> struct pair2
> {
> _T1 first;
> _T2 second;
> };
>
> template <typename T>
> class pair2_derived: public pair2<T, T>
> {
> public:
> T& xx = pair2<T, T>::first; // xx has a wrong type...what should the
> type of xx be?
> };

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

JiiPee

unread,
Dec 27, 2014, 6:45:36 PM12/27/14
to
wow. That's it! Thanks. Now I need to google why its like this :). good
to learn ....
never heard about "base"...but I ll google.

JiiPee

unread,
Dec 27, 2014, 6:48:08 PM12/27/14
to
sorry, its not about "base", its the trick typedef.... obviously it can
be like base2

JiiPee

unread,
Dec 27, 2014, 6:57:13 PM12/27/14
to
On 27/12/2014 23:34, Öö 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.

Öö Tiib

unread,
Dec 27, 2014, 7:58:42 PM12/27/14
to
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.

JiiPee

unread,
Dec 27, 2014, 8:05:14 PM12/27/14
to
so like get/set functions. I have been thinking about them, but I do not
like x() because its so long. only writing "x" is shorter. I would like
to do: obj.x rather than obj.x() . but ye there is that tradeoff. So to
fix your point I would need to not use pair but rather have x/y inside
the class, T x; .

most graphical libraries do x, so I would prefer that here... it is used
so much in the code also so it needs to be as short as possible I think.

JiiPee

unread,
Dec 27, 2014, 8:12:11 PM12/27/14
to
On 28/12/2014 00:58, Öö Tiib wrote:
> On Sunday, December 28, 2014 1:57:13 AM UTC+2, JiiPee wrote:
>> On 27/12/2014 23:34, Öö 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:
>
>

oh. was thinking/hoping the compiler would optimize that away... why
would the compiler not be able to do that?
0 new messages