13

I have several brief constexpr functions in my libraries that perform some simple calculations. I use them both in run-time and compile-time contexts.

I would like to perform some assertions in the body of these functions, however assert(...) is not valid in a constexpr function and static_assert(...) can not be used to check function parameters.

Example:

constexpr int getClamped(int mValue, int mMin, int mMax) noexcept
{
    assert(mMin <= mMax); // does not compile!
    return mValue < mMin ? mMin : (mValue > mMax ? mMax : mValue);
}

Is there a way to check whether the function is being executed in a run-time or compile-time constant and execute the assert only if it's being executed at run-time?

constexpr int getClamped(int mValue, int mMin, int mMax) noexcept
{
    assert_if_runtime(mMin <= mMax); 
    return mValue < mMin ? mMin : (mValue > mMax ? mMax : mValue);
}
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • 1
    @dasblinkenlight: What I meant is that `static_assert` [does not make sense in this occasion](http://ideone.com/6yjdAE). – Vittorio Romeo Sep 27 '14 at 08:28
  • (Disclaimer: I'm a noob and never used a constexpr in real life.) Based on my initial Google search, unless your compiler supports [N3652](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3652.html) which relaxes the C++11 `constexpr`, it can't do what you ask for. Once this is available, you will be able to throw an exception such as `std::range_error` in lieu of a `static_assert`. You can try your hand using [Clang 3.4 with std=c++14](http://clang.llvm.org/cxx_status.html). – rwong Sep 27 '14 at 09:06
  • your claim is not true for Clang in `-std=c++1y` mode, assert will work just fine. you should retag to `c++11` if you are limited to that standard. – TemplateRex Sep 27 '14 at 22:14
  • In C++20, we’ll have [`std::is_constant_evaluated`](https://en.cppreference.com/w/cpp/types/is_constant_evaluated) which would allow you to easily switch between `static_assert` and `assert` – alter_igel Jan 23 '20 at 14:59

3 Answers3

8

Throwing an exception might be useful as the compiler will ignore the run-time part when it knows at compile-time that the exception is not thrown.

#include <cassert>

constexpr int getClamped(int mValue, int mMin, int mMax)
{
    return ( mMin <= mMax ) ? 
           ( mValue < mMin ? mMin : (mValue > mMax ? mMax : mValue) ) :
           throw "mMin must be less than or equal to mMax";
}

int main( int argc, char** argv )
{
    // These two work:
    static_assert( getClamped( 42, 0, 100 ) == 42, "CT" );
    assert( getClamped( argc, 0, 100 ) == argc );

    // Fails at compile-time:
    // static_assert( getClamped( 42, 100, 0 ) == 42, "CT" );

    // Fails at run-time:
    // assert( getClamped( argc, 100, 0 ) == argc );
}

Live example

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • 2
    After some playing around (trying to find a way to make overloading on `constexpr` work), I think this is the best/only way to do it. But the thought of throwing from a clamp function horrifies me... – Ben Hymers Sep 27 '14 at 10:33
8

assert works now that g++ has implemented N3652, Relaxing constraints on constexpr functions. This status page indicates that this has been implemented in gcc5.

assert also works (in constexpr functions) on the current clang compiler shipped by Apple, with -std=c++1y.

At this time, I see nothing in the standard that assures one that assert will work in constexpr functions, and such an assurance would be a welcome addition to the standard (at least by me).

Update

Richard Smith drew my attention to LWG 2234 submitted by Daniel Krügler which is attempting to create the assurance I refer to above. This has been incorporated into C++17.

Johan
  • 74,508
  • 24
  • 191
  • 319
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • 2
    +1 I have been using asserts all over constexpr code and am very happy with it – TemplateRex Sep 27 '14 at 22:15
  • No, it won't work, because the `assert` macro must eventually call `abort` (usually via intermediate functions like __assert_fail), which is not a constexpr function. – o11c Sep 27 '14 at 22:27
  • @o11c If `assert` always called `abort`, it would be quite broken. – Potatoswatter Sep 27 '14 at 22:30
  • Relaxed `constexpr` implements support for the semicolon after `assert`, but the macro itself expands to an expression which should be usable in C++11. – Potatoswatter Sep 27 '14 at 22:31
  • @Potatoswatter Some *branch* in the `assert` must. the linked draft does not permit calls to non-constexpr expressions on any control path. The expansion of `assert(b)` is `((b) ? static_cast (0) : __assert_fail ("b", "assert.cpp", 5, __PRETTY_FUNCTION__));`, so being able to declare and mutate local variables isn't needed; the external function call is the problem. – o11c Sep 27 '14 at 22:50
  • @o11c Untaken branches don't affect constexpr. Untaken calls to non-constexpr functions have always been allowed, since the beginning in C++11. – Potatoswatter Sep 27 '14 at 22:57
7

A refinement on Daniel Frey's answer is to use noexcept on the constexpr function to turn the runtime error into a call to std::terminate. Assertion failures are unrecoverable; they should halt the process immediately. Turning them into exceptions is a very bad idea.

#include <exception>
#include <stdexcept>

struct assert_failure
  : std::logic_error
{
    explicit assert_failure(const char *sz)
      : std::logic_error(sz)
    {}
};

constexpr bool in_range(int i, int j, int k) noexcept
{
    return (i <= j && j <= k) ? true : throw assert_failure("input not in range");
}

int main(int argc, char* argv[])
{
    constexpr bool b1 = in_range(0, 4, 5); // OK!
    constexpr bool b2 = in_range(0, 6, 5); // Compile-time error!
    bool b3 = in_range(0, 4, argc);        // May or may not terminate the process
}

The runtime error for me looks like:

terminate called after throwing an instance of 'assert_failure'
  what():  input not in range
Aborted (core dumped)

Hope that helps.

Eric Niebler
  • 5,927
  • 2
  • 29
  • 43
  • 1
    +1 I explicitly removed the `noexcept`, but it does have its uses so thanks for pointing that out. Of course, it's a decision you need to make, in my usual work-environment, terminating the process is something we try to avoid but I have a lot of sympathy for fail early, fail hard :) – Daniel Frey Sep 30 '14 at 19:22