Template cross declaration ??

Started by
4 comments, last by GhostAce 17 years, 12 months ago
I have a problem with templates. Please check this code:

template<typename _TPtr, typename _TObj>
struct test_t
{
    _TPtr *ptr;
    _TObj obj;
};

typedef test_t<two_t, int>  one_t;
typedef test_t<int, one_t>  two_t;
I obviously need to declare 'two_t' before 'one_t', but 'one_t' also needs 'two_t' declared before. Code for this simple test-case can be easily written without templates, but how to make this WITH templates? I would be grateful for any help.
Advertisement
Wouldn't two_t expand to test_t<int, one_t> which would expand to test_t<int, test_t<two_t, int> > which would expand to test_t<int, test_t<test_t<int, one_t>, int> > until your compiler slaps you?

I don't get it.
Quote:Original post by MrEvil
Wouldn't two_t expand to test_t<int, one_t> which would expand to test_t<int, test_t<two_t, int> > which would expand to test_t<int, test_t<test_t<int, one_t>, int> > until your compiler slaps you?

I don't get it.


I was puzzling over this myself.

GhostAce, could you paste the non-templated code?
Notice that there is a pointer, so you won't get infinite structures recursion.

struct two_t;struct one_t{    two_t *ptr;    int obj;};struct two_t{    int *ptr;    one_t obj;};
struct FirstType;typedef std::pair<FirstType*, int> SecondType;struct FirstType : public std::pair<int*, SecondType> { };


But it's awfull. Really. I smell a design weirdness [smile]
Quote:Original post by Emmanuel Deloget
struct FirstType;typedef std::pair<FirstType*, int> SecondType;struct FirstType : public std::pair<int*, SecondType> { };


But it's awfull. Really. I smell a design weirdness [smile]



Great! [Why didn't I think of this before? :)]
Yes it's awful, but quite interesting.

Here is the corrected code if anyone is interested:
template<typename _TPtr, typename _TObj>struct test_t{    _TPtr *ptr;    _TObj obj;};struct two_t;typedef test_t<two_t, int>  one_t;typedef test_t<int, one_t>  _helper_two_t;struct two_t : public _helper_two_t {/*empty*/};

This topic is closed to new replies.

Advertisement