62

How would one properly do a static_assert within a constexpr function? For example:

constexpr int do_something(int x)
{
  static_assert(x > 0, "x must be > 0");
  return x + 5;
}

This is not valid C++11 code, because a constexpr function must only contain a return statement. I don't think that the standard has an exception to this, although, the GCC 4.7 does not let me compile this code.

2 Answers 2

71

This is not valid C++11 code, because a constexpr function must only contain a return statement.

This is incorrect. static_assert in a constexpr function are fine. What is not fine is using function parameters in constant expressions, like you do it.

You could throw if x <= 0. Calling the function in a context that requires a constant expression will then fail to compile

constexpr int do_something(int x) {
  return x > 0 ? (x + 5) : (throw std::logic_error("x must be > 0"));
}
12
  • 12
    Cool, I didn't know throws in a constexpr function that is called in a constexpr context will cause the compilation to fail!
    – Xeo
    Commented Dec 24, 2011 at 19:36
  • 18
    @Xeo doing anything non-constexpressy on the other side of ?: will do the job. :) Commented Dec 24, 2011 at 19:44
  • 6
    I'm confused why "using function parameters in constant expressions" is "not fine". If 'x' is a constant expression then (x+5) is also a constant expression and can be evaluated at compile-time. If 'x' isn't a constant expression then the function itself loses its constexpr-ness (for that particular invocation) and will simply be evaluated at run-time. Could someone clarify?
    – monkey0506
    Commented Jan 18, 2013 at 7:53
  • 6
    @monkey_05_06: Because 'x' is a function argument and therefore not a constexpr. Remember, a constexpr function must also be suitable as a runtime function (with non-constexpr args), and thus its args are ineligible for use in a static_assert(...) declaration. This is regardless of whether you ever actually call the function from a non-constexpr context, which is why the requirement that a constexpr function must also be runtime-callable is as much of a restriction as it is a "feature".
    – etherice
    Commented Apr 26, 2013 at 2:15
  • 2
    @gnzlbg: See his comment above about using anything that isn't a constexpr. What I did (as a generic solution) was declare a **non-**constexpr function template like so... template<typename RT> RT non_constexpr() { return RT{}; } ...to use like this... return (x > 0) ? (x + 5) : non_constexpr<int>(); ... tested in g++ and clang++.
    – etherice
    Commented Apr 26, 2013 at 2:43
24

This works and is valid C++11 code, because template arguments are compile time only:

template <int x>
constexpr int do_something() {
    static_assert(x > 0, "x must be > 0");
    return x + 5;
}

I faced with the same problems as you did with constant expressions in C++. There's few clear documentation about constexprs at the moment. And note that there's some known bugs with it in gcc's issue tracker, but your problem seems not to be a bug.

Note that if you declare constexpr functions inside classes, you are not able to use them inside the class. This also seems to be not a bug.

Edit: This is allowed according to the standard: 7.1.3 states

... or a compound-statement that contains only

  • null statements,
  • static_assert-declarations
  • typedef declarations and alias-declarations that do not
    define classes or enumerations,
  • using-declarations,
  • using-directives,
  • and exactly one return statement
5
  • 1
    No. constexpr must be only a single return statement. Commented Dec 14, 2012 at 22:08
  • 1
    I read the standard. You are correct, this is fine. I edited your answer to add that. Commented Dec 25, 2012 at 17:27
  • Thank you. This way works for me very well. I can not use exceptions, because they are disabled in my build (small embedded system).
    – Alexander
    Commented Dec 3, 2014 at 17:19
  • How do you call this function?
    – marsh
    Commented Sep 3, 2021 at 2:29
  • 1
    @marsh: do_something<x>(); -which means x must be a compile-time value.
    – Robert
    Commented Jan 1, 2023 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.