Recursive template

I have a problem with recursive templates.

1
2
3
4
template<class TType, int X>
class myclass{
  template<TType, MyFunc(X)> arr[MyFunc(X)];
};


This code didn`t compile, but I want to make smth like that. I want to have class which consider array of this template class. And what I need to do in MyFunc to make it works?

Thanks for help.
are you sure about your code ? i never say such as this code !!! i really want to know what is it ? however i think your code is mistake because :

template<TType, MyFunc(X)> arr[MyFunc(X)];

in C++ we dont have a code like this ! see this page maybe helpful :-??
http://aszt.inf.elte.hu/~gsd/halado_cpp/ch06s04.html
Last edited on
Like ahura says, this makes little sense. What are you trying to do? Have an array of MyFunc(X) elements that all have type TType ?
Well, you can't. There is no guarantee that what MyFunc returns only depends on it's argument, so MyFunc(X) may not be a constant even is X is. To declare an array of X elements you would
1
2
3
4
template<class TType, int X>
class myclass{
  TType arr[X];
};
Last edited on
Sorry , I was wrong in my code. I want smth like that :
1
2
3
4
5
template<class TType, int X>
class myclass{
  myclass<TType, MyFunc(X)> arr[MyFunc(X)]; //It`s no so need, but I`m very glad if you can help me
  myclass<TType, MyFunc(X)> A; // The one solution is myclass<TType, MyFunc(X)> * A;
};


The global problem is MyFunc(X), I can`t understand why it`s not compile.
MyFunc(X) is stricly decreasing(if it can help).

Sorry for my stupid questions, I can`t understand Templates well.

Thank for help.
Last edited on
this is because you cant define in your class object of itself !! Unless they are be Pointer or Static variable !!
Could the OP be looking for something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <cstdio>

template <int N>
struct Fibonacci
{
    static const int VAL =
        Fibonacci<N - 1>::VAL +
        Fibonacci<N - 2>::VAL;
};

template <>
struct Fibonacci<1> { static const int VAL = 1; };

template <>
struct Fibonacci<0> { static const int VAL = 1; };

template <int N>
struct FibonacciArray
{
    FibonacciArray<N - 1> rest_of_the_data;

    int this_data;

    FibonacciArray(): this_data(Fibonacci<N>::VAL) {}

    int operator [] (int i) const
    {
        return *( (int *)this + i);
    }
};

template<>
struct FibonacciArray<0>
{
    int this_data;

    FibonacciArray(): this_data(Fibonacci<0>::VAL) {}
};

int main()
{
    const int size = 10;

    const FibonacciArray<size> fib_arr;

    for (int i = 0; i < size; ++i)
        printf("%d\n", fib_arr[i]), fflush(stdout);

    return 0;
}

Compiling with -Os (optimization for code size) yields this gem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
	.file	"main.cpp"
	.def	___main;	.scl	2;	.type	32;	.endef
	.section .rdata,"dr"
LC0:
	.ascii "%d\12\0"
	.text
.globl _main
	.def	_main;	.scl	2;	.type	32;	.endef
_main:
	pushl	%ebp
	movl	%esp, %ebp
	andl	$-16, %esp
	pushl	%edi
	pushl	%esi
	pushl	%ebx
	subl	$68, %esp
	call	___main
	movl	$1, 20(%esp)
	movl	$1, 24(%esp)
	movl	$2, 28(%esp)
	movl	$3, 32(%esp)
	movl	$5, 36(%esp)
	movl	$8, 40(%esp)
	movl	$13, 44(%esp)
	movl	$21, 48(%esp)
	movl	$34, 52(%esp)
	movl	$55, 56(%esp)
	movl	$89, 60(%esp)
	movl	__imp___iob, %edi
	addl	$32, %edi
	xorl	%ebx, %ebx
	leal	20(%esp), %esi
L2:
	movl	(%esi,%ebx,4), %eax
	movl	%eax, 4(%esp)
	movl	$LC0, (%esp)
	call	_printf
	movl	%edi, (%esp)
	call	_fflush
	incl	%ebx
	cmpl	$10, %ebx
	jne	L2
	xorl	%eax, %eax
	addl	$68, %esp
	popl	%ebx
	popl	%esi
	popl	%edi
	leave
	ret
	.def	_printf;	.scl	2;	.type	32;	.endef
	.def	_fflush;	.scl	2;	.type	32;	.endef
Last edited on
Topic archived. No new replies allowed.