Boost logo

Boost :

Subject: Re: [boost] Adding helpful requires macros to the enable_if utility
From: pfultz2 (pfultz2_at_[hidden])
Date: 2014-06-28 12:28:41


> I find the use of macros to be far more readable than the direct use of
> templates. In fact, I was earlier toying with the idea of writing such
> macros myself. Needless to say that I would find them to be very helpful.

Besides readability, they actually help avoid the most vexing parse in
C++(something template aliases can't fix).

> Paul, can the concept be extended to a DSEL-like utility?

Well you can already write something like this on compilers that support
`constexpr`:

    template <class T>
    BOOST_FUNCTION_REQUIRES(is_foo<T>() and is_bar<T>())
    (T) foo(T t) { return t; }

This could be made to work on older compilers by using expression templates,
but
then using literals such as `is_foo<T>::value` would fail. Perhaps a second
macro like `BOOST_FUNCTION_REQUIRES_E` could be written to help avoid the
confusion. Plus, having separate `_E` macros can actually still be useful in
C++11 because of limitations of `constexpr`. For example, say we use a
`trait`
function that deduces the type parameters from a variable:

    template<template&lt;class...> class Trait, class... Ts>
    constexpr auto trait(Ts&&... xs)
    {
        return Trait(std::forward<Ts>(xs)...);
    }

    template <class T>
    T foo(T t, BOOST_REQUIRES_E(trait<is_foo>(t) and trait<is_bar>(t))) {
return t; }

This is because `trait<is_foo>(t)` won't be a `constexpr` if `t` isn't a
literal
type.

--
View this message in context: http://boost.2283326.n4.nabble.com/Adding-helpful-requires-macros-to-the-enable-if-utility-tp4664490p4664508.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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