• Advertisement
Sign in to follow this  

The template paradox

This topic is 1411 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 just wrote this, it works perfectly, but I don't know what to make of it in terms of what the concept means...

 

public class SerializedData<DataType> where DataType: SerializedData<DataType>
 
This is a template class in which its template type must be derivate of the same clase with the same template type.... sounds terribly weird

Share this post


Link to post
Share on other sites
Advertisement

each class implements its own specialized serialization, they inherit many common methods from this class and any loaded resources are held in a static hash implemented in the base serialization class.

Share this post


Link to post
Share on other sites

I don't know if this is the CRTP actually -- it looks similar-ish, but CRTP is when a a derived class inherits from a base which takes the derived class as a template parameter; here, unless I misread (which could be, I'm not sure what the language in question here is... C#? Java?), you have a templates class that requires its template argument to be of the same type.

 

OP, which language is this?

 

It would seem on the surface to be infinitely recursive, unless at some point it a default argument kicks in and bottoms out the recursion -- That, or it doesn't actually recurse at all and only goes 2 levels deep. But its hard to say -- templates/generics in any language get weird.

 

Nevermind, I see how it goes now, but I remain unconvinced that this is CRTP.

Edited by Ravyne

Share this post


Link to post
Share on other sites
Yeah, but it's the preparation for one, since C# needs this so-called generic type constraint (the part after 'where') to make it actually work. The compiler would complain otherwise. Nexus, can you confirm that you later use it like so:
 
public class SomeData : SerializedData<SomeData> ...

Share this post


Link to post
Share on other sites

It's a CRGC (curiously recurring generic constraint), not CRTP.  I don't know if anyone's ever formally called it that, but that's what I call it.

 

Like unbird says, using the CRGC requires that the parameter use a CRTP.

Edited by Nypyren

Share this post


Link to post
Share on other sites

Yes, this is the case, this class is meant to be inherited by other classes that need to be serialized and they must set themselves as the template parameter. Though it is true that this class declaration by itself does not force its data to inherit from it, it could be the same class with the same type, but that would be pretty pointless in my project.

It goes just as unbird said.

Share this post


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

  • Advertisement