SlideShare une entreprise Scribd logo
1  sur  25
Types, classes and concepts.
Nicola Bonelli :: nicola@pfq.io
Outline
● Rationale, Haskell types and type classes
● Type classes in C++?
● From Concepts to Concepts lite (C++14)
o Accu 2013 Sutton constraints
● Type classes in C++
o type class
o type class instance
o type class constraints
Haskell types and type classes
● Haskell is a pure functional language with support of parametric
polymorphism and type classes.
● Parametric polymorphism advantages:
o more intuitive that subtyping (no relationship among types)
o efficient: static polymorphism
 existential quantification enables runtime polymorphism (~ C++ inheritance + virtual fun).
● Type classes are collection of functions that represents the
interface to which certain types must adhere.
o New and existing types can be added as instances of type classes.
Typeclass: a simple example
class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool
x == y = not (x /= y)
x /= y = not (x == y)
data Maybe a = Nothing | Just a
instance (Eq a) => Eq (Maybe a) where
Nothing == Nothing = True
Just x == Just y = x == y
_ == _ = False
Class declaration
Data declaration
Instance declaration
What about C++?
● Parametric polymorphism is enabled by template (generic
programming)
● Overloading is fundamental
o semantically equivalent operations with the same name
● Abuse of inheritance
● Functions are already first-class citizen (high-order functions).
o pointer to functions, lambdas and std::function
o C++ functors (not to confuse with functors from category theory)
Type classes in C++?
● ADL, ad-hoc polymorphism (overloading) and template are not
enough!
● Functions need a support to restrict the types they take, to
improve the quality of error messages.
o very long errors with no a clear location.
● Constraining types by annotations is error prone.
o comments in source codes are not a viable solution!
Concepts!?
● C++ Concepts were proposed as an extension (to be included into C++11)
a construct that specify what a type must provide
o to codify formal properties of types (also semantically).
o to improve compiler diagnostics.
● In addition, concept map is a mechanism to bind types to concepts.
o Types can then be converted into an interface that generic functions can
utilize.
● Concepts were dropped due to some concerns.
o Mainly because they were “not ready” for C++11...
Concepts Lite!
● Simplified implementation as template constraints for functions
and classes template (in C++14 Technical specification).
o Semantic check and concept map are not implemented.
● Used to check template arguments at the point of use.
● No runtime overhead.
Constraints
● A constraint is a constexpr function template
o Can use type traits, call other constexpr functions
● Constraints check syntactic requirements
o Is this expression valid for objects of type T?
o Is the result type of an expression convertible to U?
© 2013 Andrew Sutton
Constraining template arguments
Constrain template arguments with predicates:
template<Sortable C>
void sort(C& container);
Constraints are just constexpr function templates:
template<typename T>
constexpr bool Sortable() {
return ...;
}
© 2013 Andrew Sutton
Concepts Lite
● Concept lite are intended to:
o specify properties and constraints of generic types.
o extend overloading (most constrained overload wins).
o select the most suitable class specialization.
o … enable_if is somehow deprecated.
● They do not represent the C++ version of type classes per se.
o constraint are only half of the problem.
 concept map will be not available, Stroustrup says.
Type class recipe ?!?!
To implement a type class we need:
o static_assert or concepts lite
o constraint: constexpr predicate
o definition of the typeclass
o some SFINAE tricks
o a pinch of template metaprogramming
Typeclass: multiset #1
template <typename ...Ts> struct multiset
{
static constexpr size_t size = sizeof...(Ts);
};
template <typename Set1, typename Set2> struct concat;
template <typename Set1, typename Set2>
using concat_t = typename concat<Set1, Set2>::type;
template <typename ...Ts, typename ...Vs>
struct concat<multiset<Ts...>, multiset<Vs...>>{
using type = multiset<Ts..., Vs...>;
};
Typeclass: multiset #2
template <typename T, typename Set> struct erase;
template <typename T, typename V, typename ...Ts>
struct erase<V, set<T, Ts...>>
{
using type = concat_t<set<T>, erase_t<V, set<Ts...>> >;
};
template <typename T, typename ...Ts>
struct erase<T, set<T, Ts...>> {
using type = set<Ts...>;
};
template <typename T>
struct erase<T, set<>> {
using type = set<>;
};
Typeclass: multiset #3
template <typename Set1, typename Set2> struct subtract;
template <typename ...Ts>
struct subtract< set<Ts...>, set<> > {
using type = set<Ts...>;
};
template <typename V, typename ...Ts, typename ...Vs>
struct subtract< set<Ts...>, set<V, Vs...> > {
using type = subtract_t< erase_t<V, set<Ts...>>, set<Vs...> >;
};
template <typename Set1, typename Set2>
constexpr bool equal_set()
{ return (Set1::size == Set2::size) && std::is_same<subtract_t<Set1, Set2>, set<> >::value;}
C++ type class
A typeclass is defined as a multiset of function
types:
template <typename ...Fs>
using typeclass = type::multiset<Fs...>;
User typeclass declaration:
template <typename T>
struct Show
{
static std::string show(T const &);
static std::string showList(std::list<T> const
&);
using type = typeclass
<
decltype(show),
decltype(showList)
>;
};
C++ type class instance
The typeclass_instance is defined as follow:
template <template <typename> class Class, typename T> struct typeclass_instance
{
using type = typeclass<>; // default instance is an empty typeclass.
};
The user is required to define the typeclass instance as a specialization for a given class and type:
template <>
struct typeclass_instance<Show, Test>
{
using type = typeclass <
decltype(show),
decltype(showList)
>;
};
C++ type class instance (example)
Example:
struct Test { };
std::string show(Test const &)
{ return "Test"; }
std::string showList(std::list<Test> const &)
{ return "[Test]"; }
template <>
struct typeclass_instance<Show, Test>
{
using type = typeclass <
decltype(show),
decltype(showList)
>;
};
C++ type class constraint #1
Lastly we need the constraint that ensure a type is indeed an instance of a certain typeclass:
namespace details
{
has_function_(show);
has_function_(showList);
}
template <typename T>
constexpr bool ShowInstance()
{
return type::equal_set< typename typeclass_instance<Show,Ty>::type,
typename Show<Ty>::type>() &&
details::has_function_show<T>::value &&
details::has_function_showList<std::list<T>>::value;
};
C++ type class constraint #2
Or alternatively:
template <typename T>
constexpr bool ShowInstance()
{
static_assert(!(sizeof...(Fs) < Class<Ty>::type::size), "instance declaration: incomplete interface");
static_assert(!(sizeof...(Fs) > Class<Ty>::type::size), "instance declaration: too many method");
static_assert(type::equal_set<typename typeclass_instance<Show,Ty>::type, typename Show<Ty>::type >(),
"instance declaration: function(s) mismatch");
return details::has_function_show<T>::value &&
details::has_function_showList<std::list<T>>::value;
};
C++ type class SFINAE
Where the has_function_ is a macro that generate a proper SFINAE test to check the existence of a
certain function (fun). A compiler intrinsic would be very welcome!
#define has_function_(fun) 
template <typename __Type> 
struct has_function_ ## fun 
{ 
using yes = char[1]; 
using no = char[2]; 

template <typename __Type2> static yes& check(typename std::decay<decltype(fun(std::declval<__Type2>()))>::type *); 
template <typename __Type2> static no& check(...); 

static constexpr bool value = sizeof(check<__Type>(0)) == sizeof(yes); 
};
Compiler errors #1
Incomplete instance:
using type = typeclass_instance
<
Show, Test,
decltype(show)
>;
In file included from /root/GitHub/meetup-milano-2014/cpp_typeclass/code/typeclass.cpp:5:
/root/GitHub/meetup-milano-2014/cpp_typeclass/code/./hdr/typeclass.hpp:14:5: error: static_assert failed
"instance declaration: incomplete interface"
static_assert( !(sizeof...(Fs) < Class<Ty>::type::size ), "instance declaration: incomplete
interface");
Compiler errors #2
Bad signatures:
using type = typeclass_instance
<
Show, Test,
decltype(show),
decltype(badShowList),
>;
In file included from /root/GitHub/meetup-milano-2014/cpp_typeclass/code/typeclass.cpp:5:
/root/GitHub/meetup-milano-2014/cpp_typeclass/code/./hdr/typeclass.hpp:16:5: error: static_assert failed
"instance declaration: function(s) mismatch"
static_assert( type::equal_set< type::multiset<Fs...>, typename Cl<Ty>::type >(), "TypeClass:
instance error");
Constraining a function argument type:
template <ShowInstance T>
void print(T const &elem)
{
std::cout << show (elem) << std::endl;
}
error: no matching call to ‘print(NewType &)’
note: candidate is ‘print(T& elem)’
note: where T = NewType
note: template constraints not satisfied
note: ‘T’ is not a/an ‘ShowInstance’ type
Compiler errors #3
template <typename T>
void print(T const &elem)
{
static_assert(ShowInstace<T>(),
“T not instance of Show”);
std::cout << show (elem) << std::endl;
}
Types, classes and concepts

Contenu connexe

Tendances

Csharp In Detail Part2
Csharp In Detail Part2Csharp In Detail Part2
Csharp In Detail Part2
Mohamed Krar
 
javaimplementation
javaimplementationjavaimplementation
javaimplementation
FaRaz Ahmad
 

Tendances (20)

Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1
 
What can scala puzzlers teach us
What can scala puzzlers teach usWhat can scala puzzlers teach us
What can scala puzzlers teach us
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)
 
Crystal: tipos, peculiaridades y desafios
Crystal: tipos, peculiaridades y desafiosCrystal: tipos, peculiaridades y desafios
Crystal: tipos, peculiaridades y desafios
 
Peyton jones-2011-type classes
Peyton jones-2011-type classesPeyton jones-2011-type classes
Peyton jones-2011-type classes
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Testing for share
Testing for share Testing for share
Testing for share
 
Peyton jones-2009-fun with-type_functions-slide
Peyton jones-2009-fun with-type_functions-slidePeyton jones-2009-fun with-type_functions-slide
Peyton jones-2009-fun with-type_functions-slide
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Milano JS Meetup - Gabriele Petronella - Codemotion Milan 2016
Milano JS Meetup -  Gabriele Petronella - Codemotion Milan 2016Milano JS Meetup -  Gabriele Petronella - Codemotion Milan 2016
Milano JS Meetup - Gabriele Petronella - Codemotion Milan 2016
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
 
The Future of C++
The Future of C++The Future of C++
The Future of C++
 
Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++
 
C++ Templates. SFINAE
C++ Templates. SFINAEC++ Templates. SFINAE
C++ Templates. SFINAE
 
Scala qq
Scala qqScala qq
Scala qq
 
Scala
ScalaScala
Scala
 
Effective Java - Generics
Effective Java - GenericsEffective Java - Generics
Effective Java - Generics
 
Csharp In Detail Part2
Csharp In Detail Part2Csharp In Detail Part2
Csharp In Detail Part2
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basics
 
javaimplementation
javaimplementationjavaimplementation
javaimplementation
 

En vedette

En vedette (11)

Functional approach to packet processing
Functional approach to packet processingFunctional approach to packet processing
Functional approach to packet processing
 
PFQ@ 9th Italian Networking Workshop (Courmayeur)
PFQ@ 9th Italian Networking Workshop (Courmayeur)PFQ@ 9th Italian Networking Workshop (Courmayeur)
PFQ@ 9th Italian Networking Workshop (Courmayeur)
 
PFQ@ 10th Italian Networking Workshop (Bormio)
PFQ@ 10th Italian Networking Workshop (Bormio)PFQ@ 10th Italian Networking Workshop (Bormio)
PFQ@ 10th Italian Networking Workshop (Bormio)
 
PFQ@ PAM12
PFQ@ PAM12PFQ@ PAM12
PFQ@ PAM12
 
Netmap presentation
Netmap presentationNetmap presentation
Netmap presentation
 
PF_DIRECT@TMA12
PF_DIRECT@TMA12PF_DIRECT@TMA12
PF_DIRECT@TMA12
 
DPDK KNI interface
DPDK KNI interfaceDPDK KNI interface
DPDK KNI interface
 
Understanding DPDK algorithmics
Understanding DPDK algorithmicsUnderstanding DPDK algorithmics
Understanding DPDK algorithmics
 
Vagrant
VagrantVagrant
Vagrant
 
Userspace networking
Userspace networkingUserspace networking
Userspace networking
 
Understanding DPDK
Understanding DPDKUnderstanding DPDK
Understanding DPDK
 

Similaire à Types, classes and concepts

An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++Templates
Ganesh Samarthyam
 
C++0x Variadic Type List
C++0x Variadic Type ListC++0x Variadic Type List
C++0x Variadic Type List
Akira Takahashi
 
Data structures and algorithms lab2
Data structures and algorithms lab2Data structures and algorithms lab2
Data structures and algorithms lab2
Bianca Teşilă
 

Similaire à Types, classes and concepts (20)

Templates
TemplatesTemplates
Templates
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
 
Templates2
Templates2Templates2
Templates2
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
Generic Programming in java
Generic Programming in javaGeneric Programming in java
Generic Programming in java
 
2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates2 BytesC++ course_2014_c13_ templates
2 BytesC++ course_2014_c13_ templates
 
templates.ppt
templates.ppttemplates.ppt
templates.ppt
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
 
An Introduction To C++Templates
An Introduction To C++TemplatesAn Introduction To C++Templates
An Introduction To C++Templates
 
Savitch Ch 17
Savitch Ch 17Savitch Ch 17
Savitch Ch 17
 
C++0x Variadic Type List
C++0x Variadic Type ListC++0x Variadic Type List
C++0x Variadic Type List
 
Java generics final
Java generics finalJava generics final
Java generics final
 
Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to Advanced
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
 
Data structures and algorithms lab2
Data structures and algorithms lab2Data structures and algorithms lab2
Data structures and algorithms lab2
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
Savitch ch 17
Savitch ch 17Savitch ch 17
Savitch ch 17
 
Java Generics
Java GenericsJava Generics
Java Generics
 

Dernier

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Dernier (20)

Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 

Types, classes and concepts

  • 1. Types, classes and concepts. Nicola Bonelli :: nicola@pfq.io
  • 2. Outline ● Rationale, Haskell types and type classes ● Type classes in C++? ● From Concepts to Concepts lite (C++14) o Accu 2013 Sutton constraints ● Type classes in C++ o type class o type class instance o type class constraints
  • 3. Haskell types and type classes ● Haskell is a pure functional language with support of parametric polymorphism and type classes. ● Parametric polymorphism advantages: o more intuitive that subtyping (no relationship among types) o efficient: static polymorphism  existential quantification enables runtime polymorphism (~ C++ inheritance + virtual fun). ● Type classes are collection of functions that represents the interface to which certain types must adhere. o New and existing types can be added as instances of type classes.
  • 4. Typeclass: a simple example class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool x == y = not (x /= y) x /= y = not (x == y) data Maybe a = Nothing | Just a instance (Eq a) => Eq (Maybe a) where Nothing == Nothing = True Just x == Just y = x == y _ == _ = False Class declaration Data declaration Instance declaration
  • 5. What about C++? ● Parametric polymorphism is enabled by template (generic programming) ● Overloading is fundamental o semantically equivalent operations with the same name ● Abuse of inheritance ● Functions are already first-class citizen (high-order functions). o pointer to functions, lambdas and std::function o C++ functors (not to confuse with functors from category theory)
  • 6. Type classes in C++? ● ADL, ad-hoc polymorphism (overloading) and template are not enough! ● Functions need a support to restrict the types they take, to improve the quality of error messages. o very long errors with no a clear location. ● Constraining types by annotations is error prone. o comments in source codes are not a viable solution!
  • 7. Concepts!? ● C++ Concepts were proposed as an extension (to be included into C++11) a construct that specify what a type must provide o to codify formal properties of types (also semantically). o to improve compiler diagnostics. ● In addition, concept map is a mechanism to bind types to concepts. o Types can then be converted into an interface that generic functions can utilize. ● Concepts were dropped due to some concerns. o Mainly because they were “not ready” for C++11...
  • 8. Concepts Lite! ● Simplified implementation as template constraints for functions and classes template (in C++14 Technical specification). o Semantic check and concept map are not implemented. ● Used to check template arguments at the point of use. ● No runtime overhead.
  • 9. Constraints ● A constraint is a constexpr function template o Can use type traits, call other constexpr functions ● Constraints check syntactic requirements o Is this expression valid for objects of type T? o Is the result type of an expression convertible to U? © 2013 Andrew Sutton
  • 10. Constraining template arguments Constrain template arguments with predicates: template<Sortable C> void sort(C& container); Constraints are just constexpr function templates: template<typename T> constexpr bool Sortable() { return ...; } © 2013 Andrew Sutton
  • 11. Concepts Lite ● Concept lite are intended to: o specify properties and constraints of generic types. o extend overloading (most constrained overload wins). o select the most suitable class specialization. o … enable_if is somehow deprecated. ● They do not represent the C++ version of type classes per se. o constraint are only half of the problem.  concept map will be not available, Stroustrup says.
  • 12. Type class recipe ?!?! To implement a type class we need: o static_assert or concepts lite o constraint: constexpr predicate o definition of the typeclass o some SFINAE tricks o a pinch of template metaprogramming
  • 13. Typeclass: multiset #1 template <typename ...Ts> struct multiset { static constexpr size_t size = sizeof...(Ts); }; template <typename Set1, typename Set2> struct concat; template <typename Set1, typename Set2> using concat_t = typename concat<Set1, Set2>::type; template <typename ...Ts, typename ...Vs> struct concat<multiset<Ts...>, multiset<Vs...>>{ using type = multiset<Ts..., Vs...>; };
  • 14. Typeclass: multiset #2 template <typename T, typename Set> struct erase; template <typename T, typename V, typename ...Ts> struct erase<V, set<T, Ts...>> { using type = concat_t<set<T>, erase_t<V, set<Ts...>> >; }; template <typename T, typename ...Ts> struct erase<T, set<T, Ts...>> { using type = set<Ts...>; }; template <typename T> struct erase<T, set<>> { using type = set<>; };
  • 15. Typeclass: multiset #3 template <typename Set1, typename Set2> struct subtract; template <typename ...Ts> struct subtract< set<Ts...>, set<> > { using type = set<Ts...>; }; template <typename V, typename ...Ts, typename ...Vs> struct subtract< set<Ts...>, set<V, Vs...> > { using type = subtract_t< erase_t<V, set<Ts...>>, set<Vs...> >; }; template <typename Set1, typename Set2> constexpr bool equal_set() { return (Set1::size == Set2::size) && std::is_same<subtract_t<Set1, Set2>, set<> >::value;}
  • 16. C++ type class A typeclass is defined as a multiset of function types: template <typename ...Fs> using typeclass = type::multiset<Fs...>; User typeclass declaration: template <typename T> struct Show { static std::string show(T const &); static std::string showList(std::list<T> const &); using type = typeclass < decltype(show), decltype(showList) >; };
  • 17. C++ type class instance The typeclass_instance is defined as follow: template <template <typename> class Class, typename T> struct typeclass_instance { using type = typeclass<>; // default instance is an empty typeclass. }; The user is required to define the typeclass instance as a specialization for a given class and type: template <> struct typeclass_instance<Show, Test> { using type = typeclass < decltype(show), decltype(showList) >; };
  • 18. C++ type class instance (example) Example: struct Test { }; std::string show(Test const &) { return "Test"; } std::string showList(std::list<Test> const &) { return "[Test]"; } template <> struct typeclass_instance<Show, Test> { using type = typeclass < decltype(show), decltype(showList) >; };
  • 19. C++ type class constraint #1 Lastly we need the constraint that ensure a type is indeed an instance of a certain typeclass: namespace details { has_function_(show); has_function_(showList); } template <typename T> constexpr bool ShowInstance() { return type::equal_set< typename typeclass_instance<Show,Ty>::type, typename Show<Ty>::type>() && details::has_function_show<T>::value && details::has_function_showList<std::list<T>>::value; };
  • 20. C++ type class constraint #2 Or alternatively: template <typename T> constexpr bool ShowInstance() { static_assert(!(sizeof...(Fs) < Class<Ty>::type::size), "instance declaration: incomplete interface"); static_assert(!(sizeof...(Fs) > Class<Ty>::type::size), "instance declaration: too many method"); static_assert(type::equal_set<typename typeclass_instance<Show,Ty>::type, typename Show<Ty>::type >(), "instance declaration: function(s) mismatch"); return details::has_function_show<T>::value && details::has_function_showList<std::list<T>>::value; };
  • 21. C++ type class SFINAE Where the has_function_ is a macro that generate a proper SFINAE test to check the existence of a certain function (fun). A compiler intrinsic would be very welcome! #define has_function_(fun) template <typename __Type> struct has_function_ ## fun { using yes = char[1]; using no = char[2]; template <typename __Type2> static yes& check(typename std::decay<decltype(fun(std::declval<__Type2>()))>::type *); template <typename __Type2> static no& check(...); static constexpr bool value = sizeof(check<__Type>(0)) == sizeof(yes); };
  • 22. Compiler errors #1 Incomplete instance: using type = typeclass_instance < Show, Test, decltype(show) >; In file included from /root/GitHub/meetup-milano-2014/cpp_typeclass/code/typeclass.cpp:5: /root/GitHub/meetup-milano-2014/cpp_typeclass/code/./hdr/typeclass.hpp:14:5: error: static_assert failed "instance declaration: incomplete interface" static_assert( !(sizeof...(Fs) < Class<Ty>::type::size ), "instance declaration: incomplete interface");
  • 23. Compiler errors #2 Bad signatures: using type = typeclass_instance < Show, Test, decltype(show), decltype(badShowList), >; In file included from /root/GitHub/meetup-milano-2014/cpp_typeclass/code/typeclass.cpp:5: /root/GitHub/meetup-milano-2014/cpp_typeclass/code/./hdr/typeclass.hpp:16:5: error: static_assert failed "instance declaration: function(s) mismatch" static_assert( type::equal_set< type::multiset<Fs...>, typename Cl<Ty>::type >(), "TypeClass: instance error");
  • 24. Constraining a function argument type: template <ShowInstance T> void print(T const &elem) { std::cout << show (elem) << std::endl; } error: no matching call to ‘print(NewType &)’ note: candidate is ‘print(T& elem)’ note: where T = NewType note: template constraints not satisfied note: ‘T’ is not a/an ‘ShowInstance’ type Compiler errors #3 template <typename T> void print(T const &elem) { static_assert(ShowInstace<T>(), “T not instance of Show”); std::cout << show (elem) << std::endl; }