Jump to content
  • Advertisement
Sign in to follow this  
GhostAce

Template cross declaration ??

This topic is 4501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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;
};

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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*/};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!