Boost logo

Boost :

Subject: Re: [boost] TMP error messages [ was Re: New libraries implementing C++11 features in C++03 ]
From: Ábel Sinkovics (abel_at_[hidden])
Date: 2011-11-29 13:57:24


Hi Joel,

> I've been reading the docs. This is very clever! I wonder how it
> applies to expression templates with runtime code or Fusion with
> runtime components? A simple example would be a template plus
> function: template <typename A, typename B> auto plus(A const& a, B
> const& b) -> decltype(a+b) {return a+b;} The typical problem is an
> error if a+b is not allowed (e.g. a is an int but b is a std::vector).
> I don't see how your solution will help more than static_assert or
> better yet, Boost concepts. Regards,

Thank you for checking our solution. The goal of the "compile-time
exceptions" is to be able to propagate error
messages out from a chain of recursive template metafunction calls. What
may be useful for template functions is the following:

When you use a static assertion to verify the template arguments of your
template function, you can write metafunctions as predicates that
"throw" a "compile-time exception" when the condition fails (and return
some value otherwise). You should be able to use them as predicates for
static assertions:

BOOST_MPL_ASSERT((is_exception< your predicate that may throw goes
here... >))

This will break the compilation with some error message. If the
"exception" that was "thrown" supports some pretty-printing solution
(such as
http://abel.web.elte.hu/mpllibs/metatest/index.html#_pretty_printing_custom_types)
the developer can give the predicate which has issues to a
pretty-printer in a separate compilation unit and get a
human-readable and useful error message. Concept checking conditions may
be pretty printed as well in a similar way.

As I said earlier, this solution was originally developed for returning
error messages from template metafunctions, they may be useful in
template functions as well - I'll play with it and see if they can help
there.

Regards,
   Abel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk