Boost logo

Boost :

Subject: Re: [boost] RFC Expression Validity Asserts
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2015-01-22 18:05:33


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)

********************

- Matt Calabrese


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