unit testing and error handling in c++ template ...abel.sinkovics.hu/download/test_exception.pdf ·...
TRANSCRIPT
![Page 1: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/1.jpg)
Unit testing and error handling in C++ template metaprograms
Ábel SinkovicsEndre Sajó
![Page 2: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/2.jpg)
Agenda
Introduction to C++ template metaprogramming Unit testing Testing against invalid input Compile-time exception handling
![Page 3: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/3.jpg)
What is C++ TMP?
Erwin Unruh, 1994 Turing-complete Evaluated during compilation A pure functional language!
![Page 4: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/4.jpg)
What good is C++ TMP?
Expression templates Type system extensions Concept checking DSEL's
![Page 5: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/5.jpg)
Template metafunction
template<typename T>
struct add_ptr
{
typedef T* type;
};
![Page 6: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/6.jpg)
Template metafunction
template<typename T>
struct add_ptr
{
typedef T* type;
};
Name
![Page 7: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/7.jpg)
Template metafunction
template<typename T>
struct add_ptr
{
typedef T* type;
};
Name
Parameter list
![Page 8: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/8.jpg)
Template metafunction
template<typename T>
struct add_ptr
{
typedef T* type;
};
Name
Parameter list
Return value
![Page 9: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/9.jpg)
Template metafunction
template<typename T>
struct add_ptr
{
typedef T* type;
};
Name
Parameter list
Return value
![Page 10: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/10.jpg)
Evaluating a metafunction
typedef
add_ptr<int>::type
ptr_to_int;
![Page 11: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/11.jpg)
Evaluating a metafunction
typedef
add_ptr<int>::type
ptr_to_int;
typedef
add_ptr<int>
ptr_to_int_nullary;
![Page 12: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/12.jpg)
Template metafunction class
struct add_ptr_f
{
template<typename T>
struct apply
{
typedef T* type;
};
};
![Page 13: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/13.jpg)
Template metafunction class
struct add_ptr_f
{
template<typename T>
struct apply
{
typedef T* type;
};
};
Name
![Page 14: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/14.jpg)
Template metafunction class
struct add_ptr_f
{
template<typename T>
struct apply
{
typedef T* type;
};
};
Name
Parameter list
![Page 15: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/15.jpg)
Template metafunction class
struct add_ptr_f
{
template<typename T>
struct apply
{
typedef T* type;
};
};
Name
Parameter list
Return value
![Page 16: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/16.jpg)
Template metafunction class
struct add_ptr_f
{
template<typename T>
struct apply
{
typedef T* type;
};
};
Name
Parameter list
Return value
![Page 17: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/17.jpg)
Template metafunction class
typedef
mpl::apply<add_ptr_f, int>::type
ptr_to_int;
![Page 18: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/18.jpg)
Boxed value
template<int N> struct int_ { static const int value; typedef int_ type; };
template<int N>const int int_<N>::value = N;
![Page 19: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/19.jpg)
Boxed value
template<int N> struct int_ { static const int value; typedef int_ type; };
typedef int_<3> three;
![Page 20: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/20.jpg)
Tag dispatch
template<int N>struct int_ { static const int value;
};
template<typename T>struct negate { typedef int_<-T::value> type;};
struct int_tag {};
![Page 21: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/21.jpg)
Tag dispatch
template<int N>struct int_ { static const int value; typedef int_tag tag;};
template<typename T>struct negate { typedef int_<-T::value> type;};
struct int_tag {};
![Page 22: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/22.jpg)
Tag dispatch
template<int N>struct int_ { static const int value; typedef int_tag tag;};
template<typename T>struct negate { typedef int_<-T::value> type;};
struct int_tag {};
![Page 23: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/23.jpg)
Tag dispatch
template<typename Tag>
struct negate_impl;
struct int_tag {};
![Page 24: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/24.jpg)
Tag dispatch
template<typename Tag>
struct negate_impl;
template<> struct negate_impl<int_tag> {
};
struct int_tag {};
![Page 25: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/25.jpg)
Tag dispatch
template<typename Tag>
struct negate_impl;
template<> struct negate_impl<int_tag> { template<typename T> struct apply { typedef int_<-T::value> type; }; };
struct int_tag {};
![Page 26: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/26.jpg)
Tag dispatch
template<typename T>
struct negate
: mpl::apply<
negate_impl<typename T::tag>
, T
>
struct int_tag {};
![Page 27: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/27.jpg)
Unit testing in Boost.MPL
![Page 28: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/28.jpg)
A function to test
template<typename A, typename B>
struct min
{};
![Page 29: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/29.jpg)
A function to test
template<typename A, typename B>
struct min
: if_<
typename less<A, B>::type
, A
, B
>
{};
![Page 30: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/30.jpg)
Boost.MPL assert macros
BOOST_MPL_ASSERT
BOOST_MPL_ASSERT_NOT
BOOST_MPL_ASSERT_MSG
BOOST_MPL_ASSERT_RELATION
![Page 31: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/31.jpg)
Unit test in Boost.MPL
min< int_<5>, int_<7> >::type
int_<5>
![Page 32: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/32.jpg)
Unit test in Boost.MPL
is_same<
min< int_<5>, int_<7> >::type
, int_<5>
>
![Page 33: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/33.jpg)
Unit test in Boost.MPL
BOOST_MPL_ASSERT((
is_same<
min< int_<5>, int_<7> >::type
, int_<5>
>
));
![Page 34: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/34.jpg)
Unit test in Boost.MPL
MPL_TEST_CASE()
{
BOOST_MPL_ASSERT((
is_same<
min< int_<5>, int_<7> >::type
, int_<5>
>
));
}
![Page 35: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/35.jpg)
Failed test in GCC
mintest.cpp: In function 'void test9()':
mintest.cpp:11:3: error: no matching function for call to 'assertion_failed(mpl_::failed************ boost::is_same<mpl_::int_<6>, mpl_::int_<5> >::************)'
![Page 36: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/36.jpg)
Failed test in LLVM Clangmintest.cpp:11:3: error: no matching function for call to 'assertion_failed' MPL_ASSERT(( ^~~~~~~~~~~~In file included from mintest.cpp:1:In file included from /usr/include/boost/mpl/aux_/test.hpp:19:/usr/include/boost/mpl/aux_/test/assert.hpp:20:41: note: instantiated from:#define MPL_ASSERT(pred) BOOST_MPL_ASSERT(pred) ^In file included from mintest.cpp:1:In file included from /usr/include/boost/mpl/aux_/test.hpp:19:In file included from /usr/include/boost/mpl/aux_/test/assert.hpp:17:/usr/include/boost/mpl/assert.hpp:217:32: note: instantiated from:#define BOOST_MPL_ASSERT(pred) \ ^mintest.cpp:11:3: note: instantiated from: MPL_ASSERT(( ^In file included from mintest.cpp:1:In file included from /usr/include/boost/mpl/aux_/test.hpp:19:/usr/include/boost/mpl/aux_/test/assert.hpp:20:41: note: instantiated from:#define MPL_ASSERT(pred) BOOST_MPL_ASSERT(pred) ^In file included from mintest.cpp:1:In file included from /usr/include/boost/mpl/aux_/test.hpp:19:In file included from /usr/include/boost/mpl/aux_/test/assert.hpp:17:/usr/include/boost/mpl/assert.hpp:221:11: note: instantiated from: boost::mpl::assertion_failed<false>( \ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/usr/include/boost/mpl/assert.hpp:79:5: note: candidate function [with C =false] not viable: no known conversion from 'mpl_::failed************(boost::is_same<mpl_::int_<6>, mpl_::int_<5> >::************)' to'typename assert<false>::type' (aka 'mpl_::assert<false>') for 1st argumentint assertion_failed( typename assert<C>::type ); ^1 error generated.make: *** [mintest.o] Error 1
![Page 37: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/37.jpg)
Metatest
![Page 38: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/38.jpg)
Unit tests in metatest
Type pretty-printing:
to_stream<UDT>::run(std::cout);
![Page 39: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/39.jpg)
Writing to_stream-aware classes
Specialize metafunction to_stream
![Page 40: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/40.jpg)
Writing to_stream-aware classes
Specialize metafunction to_stream
template<> struct to_stream<UDT>{
};
![Page 41: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/41.jpg)
Writing to_stream-aware classes
Specialize metafunction to_stream
template<> struct to_stream<UDT>{ static ostream& run(ostream &os) {
}};
![Page 42: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/42.jpg)
Writing to_stream-aware classes
Specialize metafunction to_stream
template<> struct to_stream<UDT>{ static ostream& run(ostream &os) { return os << ”UDT”; }};
![Page 43: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/43.jpg)
Writing to_stream-aware classes
Specialize tag-dispatched metafunction to_stream_impl
![Page 44: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/44.jpg)
Writing to_stream-aware classes
Specialize tag-dispatched metafunction to_stream_impl
template<>struct to_stream_impl<UDT_tag>{
};
![Page 45: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/45.jpg)
Writing to_stream-aware classes
Specialize tag-dispatched metafunction to_stream_impl
template<>struct to_stream_impl<UDT_tag>{ template<typename T> struct apply {
};};
![Page 46: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/46.jpg)
Writing to_stream-aware classes
Specialize tag-dispatched metafunction to_stream_impl
template<>struct to_stream_impl<UDT_tag>{ template<typename T> struct apply { static ostream& run(ostream &os) { } };};
![Page 47: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/47.jpg)
Writing to_stream-aware classes
Specialize tag-dispatched metafunction to_stream_impl
template<>struct to_stream_impl<UDT_tag>{ template<typename T> struct apply { static ostream& run(ostream &os) { return os << ”UDT”; } };};
![Page 48: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/48.jpg)
Unit test in Metatest
is_same< min< int_<5>, int_<7> >::type , int_<5> >
![Page 49: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/49.jpg)
Unit test in Metatest
typedef is_same< min< int_<5>, int_<7> >::type , int_<5> > test1;
![Page 50: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/50.jpg)
Unit test in Metatest
const suite_path suite = suite_path(”sample”)(”suite”);
typedef is_same< min< int_<5>, int_<7> >::type , int_<5> > test1;
![Page 51: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/51.jpg)
Unit test in Metatest
const suite_path suite = suite_path(”sample”)(”suite”);
typedef is_same< min< int_<5>, int_<7> >::type , int_<5> > test1;
ADD_TEST(suite, test1)
![Page 52: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/52.jpg)
Test results
Test result = success/fail status + reason Test suite structure Singleton test driver Iterator interface
![Page 53: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/53.jpg)
Test results
Test result = success/fail status + reason Test suite structure Singleton test driver Iterator interface
Ready-to-use report generators:Boost.Test, plain text, html, xml
![Page 54: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/54.jpg)
Plain text report
Passed:
the following tests have been executed: sample::suite::test1 : OK=========================Number of tests: 1Number of failures: 0
![Page 55: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/55.jpg)
Plain text report
Failed:
the following tests have been executed: sample::suite::test1 : FAIL (mintest.cpp:16) is_same<int_<6>, int_<5>>=========================Number of tests: 1Number of failures: 0
![Page 56: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/56.jpg)
Error propagation
![Page 57: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/57.jpg)
Example
template <class A, class B>struct min :
{};
![Page 58: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/58.jpg)
Example
template <class A, class B>struct min : boost::mpl::if_< >{};
![Page 59: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/59.jpg)
Example
template <class A, class B>struct min : boost::mpl::if_<less<A, B>, >{};
![Page 60: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/60.jpg)
Example
template <class A, class B>struct min : boost::mpl::if_<less<A, B>, A, B>{};
![Page 61: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/61.jpg)
Example
template <class A, class B>struct less;
template <class A, class B>struct min : boost::mpl::if_<less<A, B>, A, B>{};
![Page 62: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/62.jpg)
Example
template <class A, class B>struct less;
template <class A, class B>struct min : boost::mpl::if_<less<A, B>, A, B>{};
template <class Re, class Im>struct complex;
![Page 63: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/63.jpg)
Example
template <class A, class B>struct less;
template <class A, class B>struct min : boost::mpl::if_<less<A, B>, A, B>{};
template <class Re, class Im>struct complex;
min< complex<int_<19>, int_<83> >, // 19 + 83i complex<int_<11>, int_<13> > // 11 + 13i>
![Page 64: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/64.jpg)
Example
template <class A, class B>struct less;
template <class A, class B>struct min : boost::mpl::if_<less<A, B>, A, B>{};
template <class Re, class Im>struct complex;
min< complex<int_<19>, int_<83> >, // 19 + 83i complex<int_<11>, int_<13> > // 11 + 13i>
test.cpp: In instantiation of ‘less<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’:/usr/include/boost/mpl/if.hpp:67:11: instantiated from ‘boost::mpl::if_<less<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >, complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’test.cpp:13:36: instantiated from ‘min<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’test.cpp:21:68: instantiated from heretest.cpp:10:44: error: ‘value’ is not a member of ‘complex<mpl_::int_<11>, mpl_::int_<13> >’test.cpp: In instantiation of ‘less<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’:/usr/include/boost/mpl/if.hpp:67:11: instantiated from ‘boost::mpl::if_<less<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >, complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’test.cpp:13:36: instantiated from ‘min<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’test.cpp:21:68: instantiated from heretest.cpp:10:44: error: ‘value’ is not a member of ‘complex<mpl_::int_<19>, mpl_::int_<83> >’In file included from test.cpp:1:0:/usr/include/boost/mpl/if.hpp: In instantiation of ‘boost::mpl::if_<less<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >, complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’:test.cpp:13:36: instantiated from ‘min<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’test.cpp:21:68: instantiated from here/usr/include/boost/mpl/if.hpp:67:11: error: ‘value’ is not a member of ‘less<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’/usr/include/boost/mpl/if.hpp:70:41: error: ‘value’ is not a member of ‘less<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’
![Page 65: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/65.jpg)
Testing against invalid input
How to test a template metafunction's behaviour for invalid input?
Emit a compilation error Failed compilations mean passed tests The build system has to be prepared for it
![Page 66: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/66.jpg)
Testing against invalid input
How to test a template metafunction's behaviour for invalid input?
Emit a compilation error Failed compilations mean passed tests The build system has to be prepared for it
Return some value and throw an exception at runtime
What value to return? Will it compile? When to throw an exception?
![Page 67: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/67.jpg)
Testing against invalid input
How to test a template metafunction's behaviour for invalid input?
Emit a compilation error Failed compilations mean passed tests The build system has to be prepared for it
Return some value and throw an exception at runtime
What value to return? Will it compile? When to throw an exception?
Return a special value representing an error
![Page 68: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/68.jpg)
Returning errors
template <class Reason>struct exception{ typedef exception type;};
![Page 69: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/69.jpg)
Example
template <class A, class B>struct less : // returns either bool_<...> // or exception<values_can_not_be_compared>{};
![Page 70: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/70.jpg)
Example
template <class A, class B>struct less : // returns either bool_<...> // or exception<values_can_not_be_compared>{};
template <class A, class B>struct min : boost::mpl::if_<less<A, B>, A, B>{};
![Page 71: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/71.jpg)
Example
template <class A, class B>struct less : // returns either bool_<...> // or exception<values_can_not_be_compared>{};
template <class A, class B>struct min : boost::mpl::if_<less<A, B>, A, B>{};
template <class Re, class Im>struct complex;
min< complex<int_<19>, int_<83> >, // 19 + 83i complex<int_<11>, int_<13> > // 11 + 13i>
![Page 72: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/72.jpg)
Example
template <class A, class B>struct less : // returns either bool_<...> // or exception<values_can_not_be_compared>{};
template <class A, class B>struct min : boost::mpl::if_<less<A, B>, A, B>{};
template <class Re, class Im>struct complex;
min< complex<int_<19>, int_<83> >, // 19 + 83i complex<int_<11>, int_<13> > // 11 + 13i>
In file included from test2.cpp:1:0:/usr/include/boost/mpl/if.hpp: In instantiation of ‘boost::mpl::if_<less<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >, complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’:test2.cpp:21:36: instantiated from ‘min<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’test2.cpp:29:68: instantiated from here/usr/include/boost/mpl/if.hpp:67:11: error: ‘value’ is not a member of ‘less<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’/usr/include/boost/mpl/if.hpp:70:41: error: ‘value’ is not a member of ‘less<complex<mpl_::int_<19>, mpl_::int_<83> >, complex<mpl_::int_<11>, mpl_::int_<13> > >’
![Page 73: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/73.jpg)
Error propagation
template <class A, class B>struct min :
boost::mpl::if_<less<A, B>, A, B>
{};
template <class Re, class Im>struct complex;
min< complex<int_<19>, int_<83> >, // 19 + 83i complex<int_<11>, int_<13> > // 11 + 13i>
![Page 74: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/74.jpg)
Error propagation
template <class A, class B>struct min : boost::mpl::eval_if<
boost::mpl::if_<less<A, B>, A, B> >{};
template <class Re, class Im>struct complex;
min< complex<int_<19>, int_<83> >, // 19 + 83i complex<int_<11>, int_<13> > // 11 + 13i>
![Page 75: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/75.jpg)
Error propagation
template <class A, class B>struct min : boost::mpl::eval_if< typename is_exception<less<A, B> >::type,
boost::mpl::if_<less<A, B>, A, B> >{};
template <class Re, class Im>struct complex;
min< complex<int_<19>, int_<83> >, // 19 + 83i complex<int_<11>, int_<13> > // 11 + 13i>
![Page 76: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/76.jpg)
Error propagation
template <class A, class B>struct min : boost::mpl::eval_if< typename is_exception<less<A, B> >::type, less<A, B>, boost::mpl::if_<less<A, B>, A, B> >{};
template <class Re, class Im>struct complex;
min< complex<int_<19>, int_<83> >, // 19 + 83i complex<int_<11>, int_<13> > // 11 + 13i>
![Page 77: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/77.jpg)
Error propagation
template <class A, class B>struct min : boost::mpl::eval_if< typename is_exception<less<A, B> >::type, less<A, B>, boost::mpl::if_<less<A, B>, A, B> >{};
exception<values_can_not_be_compared>
template <class Re, class Im>struct complex;
min< complex<int_<19>, int_<83> >, // 19 + 83i complex<int_<11>, int_<13> > // 11 + 13i>
![Page 78: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/78.jpg)
Error propagation
template <class A, class B>struct min : boost::mpl::eval_if< typename is_exception<less<A, B> >::type, less<A, B>, boost::mpl::if_<less<A, B>, A, B> >{};
![Page 79: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/79.jpg)
Error propagation
template <class A, class B>struct min : boost::mpl::eval_if< typename is_exception<less<A, B> >::type, less<A, B>, boost::mpl::if_<less<A, B>, A, B> >{};
struct min_impl{ template <class LessAB> struct apply : {};};
![Page 80: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/80.jpg)
Error propagation
template <class A, class B>struct min : boost::mpl::eval_if< typename is_exception<less<A, B> >::type, less<A, B>, boost::mpl::if_<less<A, B>, A, B> >{};
struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
![Page 81: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/81.jpg)
Error propagation
template <class A, class B>struct min : boost::mpl::eval_if< typename is_exception<less<A, B> >::type, less<A, B>, boost::mpl::if_<less<A, B>, A, B> >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
![Page 82: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/82.jpg)
Error propagation
template <class A, class B>struct min : boost::mpl::eval_if< typename is_exception<less<A, B> >::type, less<A, B>, min_impl<A, B> >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
![Page 83: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/83.jpg)
Error propagation
template <class A, class B>struct min : boost::mpl::eval_if< typename is_exception<less<A, B> >::type, less<A, B>, boost::mpl::apply<min_impl<A, B>, less<A, B> > >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
![Page 84: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/84.jpg)
Error propagation
template <class A, class B>struct min : boost::mpl::eval_if< typename is_exception<less<A, B> >::type, less<A, B>, boost::mpl::apply<min_impl<A, B>, less<A, B> > >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
![Page 85: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/85.jpg)
Error propagation
template <class A, class B>struct min : boost::mpl::eval_if< typename is_exception<less<A, B> >::type, less<A, B>, boost::mpl::apply<min_impl<A, B>, less<A, B> > >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
![Page 86: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/86.jpg)
Error propagation
template <class X, class B>struct min : boost::mpl::eval_if< typename is_exception< X >::type, X , boost::mpl::apply<min_impl<A, B>, X > >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
![Page 87: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/87.jpg)
Error propagation
template <class X, class F>struct min : boost::mpl::eval_if< typename is_exception< X >::type, X , boost::mpl::apply< F , X > >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
![Page 88: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/88.jpg)
Error propagation
template <class X, class F>struct bind_exception : boost::mpl::eval_if< typename is_exception< X >::type, X , boost::mpl::apply< F , X > >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
![Page 89: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/89.jpg)
Error propagation
template <class X, class F>struct bind_exception : boost::mpl::eval_if< typename is_exception< X >::type, X , boost::mpl::apply< F , X > >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
template <class A, class B>struct min : bind_exception< less<A, B>, min_impl<A, B> >{};
![Page 90: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/90.jpg)
Error propagation
template <class X, class F>struct bind_exception : boost::mpl::eval_if< typename is_exception< X >::type, X , boost::mpl::apply< F , X > >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
template <class A, class B>struct min : bind_exception< less<A, B>, min_impl<A, B> >{};
min<A, B>
![Page 91: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/91.jpg)
Error propagation
template <class X, class F>struct bind_exception : boost::mpl::eval_if< typename is_exception< X >::type, X , boost::mpl::apply< F , X > >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
template <class A, class B>struct min : bind_exception< less<A, B>, min_impl<A, B> >{};
less<A, B> min_impl<A, B>
min<A, B>
![Page 92: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/92.jpg)
Error propagation
template <class X, class F>struct bind_exception : boost::mpl::eval_if< typename is_exception< X >::type, X , boost::mpl::apply< F , X > >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
template <class A, class B>struct min : bind_exception< less<A, B>, min_impl<A, B> >{};
less<A, B> min_impl<A, B>
min<A, B>
![Page 93: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/93.jpg)
Error propagation
template <class X, class F>struct bind_exception : boost::mpl::eval_if< typename is_exception< X >::type, X , boost::mpl::apply< F , X > >{};
template <class A, class B>struct min_impl{ template <class LessAB> struct apply : boost::mpl::if_<LessAB, A, B> {};};
template <class A, class B>struct min : bind_exception< less<A, B>, min_impl<A, B> >{};
less<A, B> min_impl<A, B>
bind_exception
min<A, B>
![Page 94: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/94.jpg)
Error propagation
A higher-order function: × →
template <class X, class F>struct bind_exception;
![Page 95: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/95.jpg)
Error propagation
A higher-order function:error/result × →
template <class X, class F>struct bind_exception;
![Page 96: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/96.jpg)
Error propagation
A higher-order function:error/result × (value error/result) → →
template <class X, class F>struct bind_exception;
![Page 97: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/97.jpg)
Error propagation
A higher-order function:error/result × (value error/result) error/result→ →
template <class X, class F>struct bind_exception;
![Page 98: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/98.jpg)
Error propagation
Chaining functionserror/result × (value error/result) error/result→ →
![Page 99: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/99.jpg)
Error propagation
Chaining functionserror/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
![Page 100: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/100.jpg)
Error propagation
Chaining functionserror/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
![Page 101: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/101.jpg)
Error propagation
Chaining functionserror/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
![Page 102: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/102.jpg)
Error propagation
Chaining functionserror/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
bind_exception
![Page 103: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/103.jpg)
Error propagation
Chaining functionserror/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
bind_exception
![Page 104: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/104.jpg)
Error propagation
Chaining functionserror/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
bind_exception
bind_exception
![Page 105: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/105.jpg)
Error propagation
Chaining functionserror/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
![Page 106: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/106.jpg)
Error propagation
Chaining functionserror/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
Business logic
![Page 107: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/107.jpg)
Error propagation
Chaining functionserror/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
error/result × (value error/result) error/result→ →
Business logic
less
min_impl
...
...
![Page 108: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/108.jpg)
Error propagation
template <class A, class B, class C, class D>struct min4 : min<min<A, B>, min<C, D> >{};
![Page 109: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/109.jpg)
Error propagation
template <class A, class B, class C, class D>struct min4 : min<min<A, B>, min<C, D> >{};
template <class A, class B, class C, class D>struct min4 :
{};
![Page 110: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/110.jpg)
Error propagation
template <class A, class B, class C, class D>struct min4 : min<min<A, B>, min<C, D> >{};
template <class A, class B, class C, class D>struct min4 : bind_exception< min<C, D>,
>{};
![Page 111: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/111.jpg)
Error propagation
template <class A, class B, class C, class D>struct min4 : min<min<A, B>, min<C, D> >{};
template <class A, class B, class C, class D>struct min4 : bind_exception< min<C, D>, bind_exception< min<A, B>, > >{};
![Page 112: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/112.jpg)
Error propagation
template <class A, class B, class C, class D>struct min4 : min<min<A, B>, min<C, D> >{};
template <class A, class B, class C, class D>struct min4 : bind_exception< min<C, D>, bind_exception< min<A, B>, curry<min> > >{};
![Page 113: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/113.jpg)
Error propagation
template <class A, class B, class C, class D>struct min4 : min<min<A, B>, min<C, D> >{};
template <class A, class B, class C, class D>struct min4 : bind_exception< min<C, D>, bind_exception< min<A, B>, curry<min> > >{};
try_< … >
![Page 114: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/114.jpg)
Error propagation
template <class A, class B, class C, class D>struct min4 : min<min<A, B>, min<C, D> >{};
template <class A, class B, class C, class D>struct min4 : bind_exception< min<C, D>, bind_exception< min<A, B>, curry<min> > >{};
try_< … >
template <class A, class B, class C, class D>struct min4 : try_<min<min<A, B>, min<C, D> > >{};
![Page 115: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/115.jpg)
Testing
Unit testing frameworks can catch exceptions Compilation succeeds They can be part of the test report
In the ”Real World” nothing catches the exception
Compilation breaks It is easy to build tools pretty-printing the exception
![Page 116: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/116.jpg)
Not covered...
Exceptions at compile-time One can throw_<...> One can catch_<...>
Monads Exception handling is implemented using monads This is not their only use case in C++ template
metaprogramming
![Page 117: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/117.jpg)
Summary
C++ template metaprogramming is useful Developing metaprograms is difficult
Lack of proper unit testing tools Lack of handling errors at compile-time ...
![Page 118: Unit testing and error handling in C++ template ...abel.sinkovics.hu/download/test_exception.pdf · Agenda Introduction to C++ template metaprogramming Unit testing Testing against](https://reader034.vdocument.in/reader034/viewer/2022050314/5f77162949a2bc02c565882c/html5/thumbnails/118.jpg)
Summary
C++ template metaprogramming is useful Developing metaprograms is difficult
Lack of proper unit testing tools Lack of handling errors at compile-time ...