Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modelling Typeclasses in C++

Is it possible to implement Haskell typeclasses in C++? If yes, then how?

like image 258
Sunil Kothari Avatar asked Jun 27 '10 18:06

Sunil Kothari


2 Answers

There's a few papers on this, which might be useful as background reading:

  • C++ templates/traits versus Haskell type classes, Sunil Kothari , Martin Sulzmann
  • A Comparative Study of Language Support for Generic Programming, Ronald Garcia , Jaakko Järvi , Andrew Lumsdaine , Jeremy Siek , Jeremiah Willcock
like image 73
Don Stewart Avatar answered Nov 16 '22 07:11

Don Stewart


The similar mechanism in C++ is called "concepts". The idea is to define a typeclass by defining the requirements of any type belonging to that class. C++ iterators make extensive use of concepts, and C++0x had intended to support direct syntax for them (rather than the indirect template tricks to perform concept checks C++ currently employs), but it appears this support has been dropped from the standard.

like image 39
John Avatar answered Nov 16 '22 05:11

John