SlideShare a Scribd company logo
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

More Related Content

PDF
Cat's anatomy
PDF
Introduction to Scala
PDF
An introduction to functional programming with Swift
PPTX
Java New Programming Features
PDF
It's All About Morphisms
PDF
Generic Programming
PDF
Cheat Sheet java
PDF
Effective Modern C++
Cat's anatomy
Introduction to Scala
An introduction to functional programming with Swift
Java New Programming Features
It's All About Morphisms
Generic Programming
Cheat Sheet java
Effective Modern C++

What's hot (20)

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

Viewers also liked (11)

PDF
Functional approach to packet processing
PPT
PFQ@ 9th Italian Networking Workshop (Courmayeur)
PDF
PFQ@ 10th Italian Networking Workshop (Bormio)
PPT
PFQ@ PAM12
PPTX
Netmap presentation
PPT
PF_DIRECT@TMA12
PPTX
DPDK KNI interface
PPTX
Understanding DPDK algorithmics
PPTX
PDF
Userspace networking
PPTX
Understanding DPDK
Functional approach to packet processing
PFQ@ 9th Italian Networking Workshop (Courmayeur)
PFQ@ 10th Italian Networking Workshop (Bormio)
PFQ@ PAM12
Netmap presentation
PF_DIRECT@TMA12
DPDK KNI interface
Understanding DPDK algorithmics
Userspace networking
Understanding DPDK
Ad

Similar to Types, classes and concepts (20)

PDF
C++20 新功能:Concepts & Ranges
PPTX
RTTI and Namespaces.pptx ppt of c++ programming language
PDF
Generic programming and concepts that should be in C++
PDF
Let's get comfortable with C++20 concepts (XM)
PDF
Compiler Construction | Lecture 7 | Type Checking
PDF
Let's get comfortable with C++20 concepts - Cologne C++ User group
PPT
2CPP15 - Templates
PDF
[gbgcpp] Let's get comfortable with concepts
PDF
Unit 2 Methods and Polymorphism-Object oriented programming
PDF
Beyond C++17
PDF
A Brief Introduction to Type Constraints
PPTX
Data structures and algorithms lab2
PDF
Cpp17 and Beyond
PPTX
Polymorphism & Templates
PDF
The Present and The Future of Functional Programming in C++
PDF
C++11
PPTX
C++11 - A Change in Style - v2.0
PPT
Friend this-new&delete
PDF
麻省理工C++公开教学课程(二)
PDF
C++ Standard Template Library
C++20 新功能:Concepts & Ranges
RTTI and Namespaces.pptx ppt of c++ programming language
Generic programming and concepts that should be in C++
Let's get comfortable with C++20 concepts (XM)
Compiler Construction | Lecture 7 | Type Checking
Let's get comfortable with C++20 concepts - Cologne C++ User group
2CPP15 - Templates
[gbgcpp] Let's get comfortable with concepts
Unit 2 Methods and Polymorphism-Object oriented programming
Beyond C++17
A Brief Introduction to Type Constraints
Data structures and algorithms lab2
Cpp17 and Beyond
Polymorphism & Templates
The Present and The Future of Functional Programming in C++
C++11
C++11 - A Change in Style - v2.0
Friend this-new&delete
麻省理工C++公开教学课程(二)
C++ Standard Template Library

Recently uploaded (20)

PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
top salesforce developer skills in 2025.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
history of c programming in notes for students .pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
System and Network Administraation Chapter 3
PDF
AI in Product Development-omnex systems
PDF
Nekopoi APK 2025 free lastest update
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Transform Your Business with a Software ERP System
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Odoo POS Development Services by CandidRoot Solutions
top salesforce developer skills in 2025.pdf
PTS Company Brochure 2025 (1).pdf.......
Odoo Companies in India – Driving Business Transformation.pdf
Softaken Excel to vCard Converter Software.pdf
history of c programming in notes for students .pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
System and Network Administraation Chapter 3
AI in Product Development-omnex systems
Nekopoi APK 2025 free lastest update
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Transform Your Business with a Software ERP System
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Design an Analysis of Algorithms I-SECS-1021-03
Design an Analysis of Algorithms II-SECS-1021-03
How Creative Agencies Leverage Project Management Software.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf

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; }