The template paradox

Started by
7 comments, last by NEXUSKill 10 years, 1 month ago

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
Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


Advertisement

Why would you serialize your own serializing class?

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

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.

Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


It has a name (and of course its usefulness) : curiously recurring template pattern.

Oh god I laughed so hard when I read the pattern name! xD

Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


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.

throw table_exception("(? ???)? ? ???");

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> ...

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.

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.

Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


This topic is closed to new replies.

Advertisement