Boost logo

Boost :

Subject: Re: [boost] RFC Expression Validity Asserts
From: Edward Diener (eldiener_at_[hidden])
Date: 2015-01-22 18:49:05


On 1/22/2015 6:05 PM, Matt Calabrese wrote:
> On Wed, Jan 21, 2015 at 4:52 PM, Matt Calabrese <rivorus_at_[hidden]> wrote:
>
>> I've found it useful, especially in tests, to easily be able to do
>> expression validity checking via SFINAE in a static assert.
>>
>
> Bump, and also a couple of additional macros for checking constexpr-ness of
> an expression are linked farther down in this email, using similar tricks
> as the SFINAE checks int the previous email (pushing more complicated
> checks into the body of a lambda such that the macro expands to an
> expression).
>
> Motivation for this is that often when writing templated code it is
> difficult to be certain that a specific instantiation is actually constexpr
> even if the constexpr keyword is used with the function template. For
> instance, for a matrix type whose value type is a template parameter T, it
> can be difficult to know if matrix<T>'s multiplication operation actually
> results in a constexpr object, even if it has constexpr inputs.
> Particularly when designing such a library, it is useful to be able to
> write simple tests that check the constexpr-ness of a specified expression
> in the form of a static_assert. A proof-of-concept implementation of this
> is linked below:
>
> http://ideone.com/ko09KQ
>
>
> As an example of such a usage:
>
> ////////////////////
>
> int foo(int) { return 0; }
>
> STATIC_ASSERT_CONSTEXPR
> (
> (constexpr int) var = 5,
> foo(var)
> );
>
> ////////////////////
>
> Produces the error:
>
> prog.cpp:70:1: error: static assertion failed:
>
> ********************
>
> Given:
> constexpr int var = 5;
>
> The following expression was expected to be constexpr but was not:
> foo(var)
>
> ********************

This looks interesting but I am confused about where this implementation
is. Is it available somewhere ? is it documented at all ? Are there
examples/tests ? Is there a good general of when some of these macros
should/would be used in template code ?

It looks like you are saying that in place of BOOST_STATIC_ASSERT one
can use your macros at compile time to produce compile time errors, but
the majority of C++ expressions have to be evaluated at run-time. So is
this strictly for compile-time expressions or what ? Your original OP
looked like it worked with run-time expressions ( ++a, a+b etc. ).


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