Is it possible to implement Haskell typeclasses in C++? If yes, then how?
+4
A:
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
Don Stewart
2010-06-27 18:23:24
Excellent papers. Thanks.
Sunil Kothari
2010-06-27 18:38:00
Am I the only one to notice that the OP's name is similar to the name of the first author of "C++ templates/traits versus Haskell type classes" ?
Matthieu M.
2010-06-30 11:19:42
+1 for the second link, a categorization of the required features is very interesting indeed.
Matthieu M.
2010-06-30 12:10:54
First paper: "C++ templates/traits versus Haskell type classes". There is an error at page 9 (Example 6), code is expressed in the body of a `struct`. It seems to me that the author have only an approximative knowledge of C++ and this shows in the examples.
Matthieu M.
2010-06-30 12:32:28
+1
A:
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.
John
2010-06-27 19:07:21