Boost logo

Boost :

Subject: Re: [boost] status of type traits addition (has_operator_xxx)
From: Jeffrey Lee Hellrung, Jr. (jhellrung_at_[hidden])
Date: 2011-01-13 20:27:46


On 1/13/2011 12:09 PM, Frédéric Bron wrote:
> Here is the status of the type-traits addition:
> - I added the following operators: +=, *=, -=, /*, %=,&=, |=, ^=,<<,
>>> ,>>=,<<=, prefix ++, prefix --, postfix ++, postfix -- so that the
> covered operators are now:
> - I added in the doc. the fact that private operators are not covered
> and lead to compile time errors
> - for operators on built-in types that require integer arguments (%,
> bitwise op.): I replaced a long list of specialiazation by an
> automatic detection

Sounds good.

> Remaining work:
[...]
> 3. have an MPL predicate for return value
>
> As I have only little time (full time work and 3 young children...), I will:
[...]
> - I will certainly leave 3. to somebody else in the future because I
> am not enough agile with MPL,
[...]

Interface decisions aside, the mechanism to implement 3. is not *too*
hard (but can be rather tedious) relative to the infrastructure you've
developed to this point. The most you'll need from Boost.MPL for the
implementation is mpl::apply (and maybe mpl::always for the default
predicate).

In the end, it all boils down to determining if the type of an
expression satisfies a given Boost.MPL predicate. This is relatively
straightforward if you use decltype (I *think*; I have not actually
tested it). For example, suppose that you want to determine if
declval<T>() + declval<U>() has exactly the type V. Then

static const bool value = boost::mpl::apply< boost::is_same<
boost::mpl::_1, V >, decltype( declval<T>() + declval<U>() ) >::type::value;

will set value to appropriately answer your query. Obviously, you can
replace boost::is_same< boost::mpl::_1, V > with any Boost.MPL
metafunction predicate (i.e., it should come from a template parameter),
and declval<T>() + declval<U>() can be any C++ expression (i.e., it
should depend on the specific operator trait in question).

In the absence of C++0x decltype, it's still possible, but it's quite a
bit more involved. One has to *first* determine whether the expression
has a void type, and if it doesn't, one can deduce the type of the
expression through a function template and communicate the result of
applying the Boost.MPL metafunction predicate back via the sizeof trick.
  And one should aim to correctly distinguish rvalues and lvalues.

Since (I believe) I originally suggested 3., I'll help out however you
think I can. E.g., I can extract what I use currently and send you
sample code privately, if you wish.

- Jeff


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