Created
January 27, 2014 00:16
-
-
Save cdleary/8641252 to your computer and use it in GitHub Desktop.
crtp example from blog entry http://blog.cdleary.com/2012/01/c-generic-wrappers-and-crtp-oh-mi/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Cake { | |
static Cake Fail; | |
bool burned(); | |
}; | |
Cake Cake::Fail; | |
struct Ingredients { bool spoiled(); }; | |
struct BatterAndWhatnot {}; | |
template <class Wrapped> | |
class CakeMaker | |
{ | |
public: | |
Cake makeCake() { | |
Wrapped *self = static_cast<Wrapped *>(this); | |
Ingredients ingredients = self->fetchIngredients(); | |
if (ingredients.spoiled()) | |
return Cake::Fail; | |
BatterAndWhatnot batter = self->mixAndStuff(ingredients); | |
Cake result = self->bake(batter); | |
if (result.burned()) | |
return Cake::Fail; | |
return result; | |
} | |
}; | |
class PersonalChef : public CakeMaker<PersonalChef> | |
{ | |
Ingredients fetchIngredients(); | |
BatterAndWhatnot mixAndStuff(Ingredients); | |
Cake bake(BatterAndWhatnot); | |
friend class CakeMaker; | |
}; | |
int main() | |
{ | |
PersonalChef chef; | |
chef.makeCake(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment