473,511 Members | 16,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can we point to itself in template parameter

Grizlyk wrote:

I need to point self class in template parameter, for example

template <
class Tptr,
class Tc_iterator
>
class Vcontainer
{
public:
virtual void add_befor(Tc_iterator&, const Tptr);

virtual ~Vcontainer(){}
};

template <
class Tptr,
class Tc_container
>
class Viterator
{
public:
virtual char next(const char is_check_bound=0);

Tc_container *container;

virtual ~Viterator(){}
};

class Tptr;
Viterator< Tptr, Vcontainer<Tptr,"Viterator" a;

How can we point to itself here: "Viterator" (after class "Viterator" has
been declared)?

We need only name of class in the case. It can be easy done for non-template
class

class X
{
public:
X* next; //I can do it
};
--
Maksim A Polyanin
Jan 28 '07 #1
9 1612


On Jan 28, 2:52 pm, "Grizlyk" <grizl...@yandex.ruwrote:
Grizlyk wrote:I need to point self class in template parameter, for example

template <
class Tptr,
class Tc_iterator
>
class Vcontainer
{
public:
virtual void add_befor(Tc_iterator&, const Tptr);

virtual ~Vcontainer(){}

};template <
class Tptr,
class Tc_container
>
class Viterator
{
public:
virtual char next(const char is_check_bound=0);

Tc_container *container;

virtual ~Viterator(){}

};class Tptr;
Viterator< Tptr, Vcontainer<Tptr,"Viterator" a;

How can we point to itself here: "Viterator" (after class "Viterator" has
been declared)?

We need only name of class in the case. It can be easy done for non-template
class

class X
{
public:
X* next; //I can do it

};--
Maksim A Polyanin
A template is not a typename unless its arguments are evaluated
first .So you can not recurse when sending parameters to a
template.BUT I just successfully tested the following nestrick
(nesting trick) on VC7.I wanted to pass the "generic" template to
itself as an argument:

template <typename ty>
struct generic{
typedef typename ty::TResult * TPointer;
TPointer ptr;
};

template<typename ty>
struct type{//for every other type
typedef ty TResult;
};

struct generic_self{//for generic itself only
typedef generic<generic_selfTResult;
};

generic< type<int Gi; //a generic for int
generic< generic_self GG; //a generic for generic

Jan 28 '07 #2


On Jan 28, 2:52 pm, "Grizlyk" <grizl...@yandex.ruwrote:
Grizlyk wrote:I need to point self class in template parameter, for example

template <
class Tptr,
class Tc_iterator
>
class Vcontainer
{
public:
virtual void add_befor(Tc_iterator&, const Tptr);

virtual ~Vcontainer(){}

};template <
class Tptr,
class Tc_container
>
class Viterator
{
public:
virtual char next(const char is_check_bound=0);

Tc_container *container;

virtual ~Viterator(){}

};class Tptr;
Viterator< Tptr, Vcontainer<Tptr,"Viterator" a;

How can we point to itself here: "Viterator" (after class "Viterator" has
been declared)?

We need only name of class in the case. It can be easy done for non-template
class

class X
{
public:
X* next; //I can do it

};--
Maksim A Polyanin
A template is not a typename unless its arguments are evaluated
first .So you can not recurse when sending parameters to a
template.BUT I just successfully tested the following nestrick
(nesting trick) on VC7.I wanted to pass the "generic" template to
itself as an argument:

template <typename ty>
struct generic{
typedef typename ty::TResult * TPointer;
TPointer ptr;
};

template<typename ty>
struct type{//for every other type
typedef ty TResult;
};

struct generic_self{//for generic itself only
typedef generic<generic_selfTResult;
};

generic< type<int Gi; //a generic for int
generic< generic_self GG; //a generic for generic

Jan 28 '07 #3


On Jan 28, 3:52 am, "Grizlyk" <grizl...@yandex.ruwrote:
Grizlyk wrote:I need to point self class in template parameter, for example
template <
class Tptr,
class Tc_container
>
class Viterator
{
public:
virtual char next(const char is_check_bound=0);

Tc_container *container;

virtual ~Viterator(){}

};

class Tptr;
Viterator< Tptr, Vcontainer<Tptr,"Viterator" a;

How can we point to itself here: "Viterator" (after class "Viterator" has
been declared)?
There is no class named Viterator - only a class template named
Viterator. And a class template is not a class - nor even a type.

Now it is possible to create classes from the Viterator class
template. Classes created in this way are named VIterator<Type1,
Type2where Type1 and Type2 are the names of the parameterized types
used to instantiate the Viterator class template.
We need only name of class in the case. It can be easy done for non-template
class

class X
{
public:
X* next; //I can do it

};--
Viterator can do the same:

template <class T1, class T2>
class Viterator
{
public:
Viterator * next;
...
};

But the actual request in this case is entirely different.
Essentially, the aim is to instantiate the class template,
Viterator,with two types: T1 and T2, and have T2 be the type:
Viterator<T1, T2>. In other words T2 in Viterator<T1, T2is supposed
to be none other than Viterator<T1, T2itself.

Well, it would be a neat trick if it could be pulled off, but sadly,
it's not possible in C++ to instantiate a class template with the
complete, instantiated type as one of its own type parameters. In
other words, in order to instantaite Viterator in this case, the
program must first instantiate Viterator, but before that can be done,
the program must first instantiate Viterator - and so on and so forth.
So even if this kind of self-referential instantiation were allowed in
C++ - the infinite recursion involved would translate into a long wait
for the program to finish compiling.

Greg

Jan 28 '07 #4
Greg wrote:
>
So even if this kind of self-referential instantiation were allowed in
C++ - the infinite recursion involved would translate into a long wait
for the program to finish compiling.

Not of course, no recursion here. In my example class template parameter
refering to self class, so compiler can delay generating concrete class till
all class declaration will be read (to point "};").

C++ must allow to do something like this
typedef Viterator< Tptr, Vcontainer<Tptr,Viterator Tc_iterator;

Using a template name without template parameters is allowed in other
places, for example
virtual ~Viterator(){} // not Viterator< T1, T2 >

We can do something like this manualy (it can be compiled)

template <
class Tptr,
class Tc_iterator
>
class Vcontainer
{
public:
virtual void add_befor(Tc_iterator&, const Tptr);

virtual ~Vcontainer(){}
};

template <
class Tptr,
class Tc_container
>
class Viterator
{
public:
virtual char next(const char is_check_bound=0);

Tc_container *container;

virtual ~Viterator(){}
};

class Tptr;

//typedef Viterator< Tptr, Vcontainer<Tptr,Viterator Tc_iterator;
class Tc_iterator;

Viterator< Tptr, Vcontainer<Tptr,Tc_iterator a;

//at the point inheritance do nothing except introducing new name
class Tc_iterator: public Viterator< Tptr, Vcontainer<Tptr,Tc_iterator
{
};

class Tptr{};
typedef Viterator< Tptr, Vcontainer<Tptr,Viterator Tc_iterator2;

Vcontainer < Tptr, Tc_iterator2 b;

void foo()
{
a.container=0;
b.add_befor(a,Tptr());
}

--
Maksim A Polyanin
Jan 28 '07 #5
class Tptr{};
typedef Viterator< Tptr, Vcontainer<Tptr,Viterator Tc_iterator2;
No, error
typedef Viterator< Tptr, Vcontainer<Tptr,Tc_iterator Tc_iterator2;
Vcontainer < Tptr, Tc_iterator2 b;

void foo()
{
a.container=0;
b.add_befor(a,Tptr());
}
--
Maksim A Polyanin
Jan 28 '07 #6
Maybe like this:

//make new name with the help of inheritance
template<class Tptr>
class Tc_iterator:
public Viterator<
Tptr,
Vcontainer<Tptr,Tc_iterator<Tptr
>
{
};

class Tptr{};
Tc_iterator<Tptr a;
Vcontainer < Tptr, Tc_iterator<Tptr b;

int main()
{
a.container=&b;
b.add_befor(a,Tptr());
}

--
Maksim A Polyanin
Jan 28 '07 #7


On Jan 28, 6:52 pm, "Grizlyk" <grizl...@yandex.ruwrote:
Maybe like this:

//make new name with the help of inheritance
template<class Tptr>
class Tc_iterator:
public Viterator<
Tptr,
Vcontainer<Tptr,Tc_iterator<Tptr
>
{

};class Tptr{};
Tc_iterator<Tptr a;
Vcontainer < Tptr, Tc_iterator<Tptr b;

int main()
{
a.container=&b;
b.add_befor(a,Tptr());

}--
Maksim A Polyanin
You lose a lot in this way the constructors are not usable after
inheritance and actualy you are using a different type which might
cause pointer casting problems later.If you think that you might need
to pass the the type as the argument to itself its better to device
the trick I mentioned in my previous post.Moreover most of the time
such programs behave like worms because of program rational defects ;
this case is different from self pointing classes you pointed in your
previous post.
>We need only name of class in the case. It can be easy done for non-template
class
>class X
{
public:
X* next; //I can do it
};

Jan 28 '07 #8
terminator wrote:
>
If you think that you might need
to pass the the type as the argument to itself its better to device
the trick I mentioned in my previous post.
Yes.

--
Maksim A Polyanin
Jan 28 '07 #9


On Jan 28, 8:26 pm, "Grizlyk" <grizl...@yandex.ruwrote:
terminator wrote:
If you think that you might need
to pass the the type as the argument to itself its better to device
the trick I mentioned in my previous post.
Yes.

--
Maksim A Polyanin
check this too:
template <typename traits>
struct generic2 {
typedef typename traits::first first;
typedef typename traits::second second;
first * ptr1;
second * ptr2;
};

template <typename A,typename B>
struct for_2_types{
typedef A first;
typedef B second;
};

template <typename B>
struct generic_self_1{
typedef generic2<generic_self_1first;
typedef B second;
};

template <typename A>
struct generic_self_2{
typedef A first;
typedef generic2<generic_self_2second;
};
struct generic_self_both{
typedef generic2<generic_self_bothfirst,second;
};

// generic2 for int,double
generic2<for_2_types<int,double G_id;

// generic2 for generic2,float
generic2<generic_self_1<float G_Gf;

// generic2 for char,generic2
generic2<generic_self_2<char G_cG;

// generic2 for generic2,generic2
generic2<generic_self_both > G_GG;
Jan 29 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2117
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
8
3135
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
2
2743
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
3
1726
by: Erik Wikström | last post by:
I've been trying for a while now to understand how template template parameters work. But I just can't wrap my head around it and was hoping that someone might help me. As best I can figure the...
6
3731
by: aaragon | last post by:
Hi everyone, I found an example of a Point class in another message in the group but I can't compile with gnu g++. Anyone has any clue why it doesn't work? The code for the class and the error...
8
5303
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
3
1663
by: Anonymous | last post by:
Could someone please explain template template classes, showing: 1). Why they are needed / i.e what problem do they solve ? 2). A simple example I have read various articles etc, but it still...
4
3003
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the...
2
2361
by: ndbecker2 | last post by:
On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is now rejected: #include <vector> #include <iostream> template<typename T, template <typename Aclass CONT=std::vector>...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.