Skip to content

Last updated:2026-10-12

C++20 constexpr and the Comma Expression

C++那些事: constexpr and comma expr

C++20 introduced a number of new features that further strengthen compile-time capabilities and code safety. This article takes a deep dive into two important changes: exception handling in constexpr functions and the deprecation of the comma operator in subscripts.

Note: for the lazy version, the code examples and related documentation for this section have been updated in the Knowledge Planet — join if you are interested.

1. The Change Supporting try-catch Exception Handling in constexpr Functions

Before C++20, try-catch blocks could not be used inside constexpr functions, a restriction that held back the compile-time capabilities of some standard library containers such as std::array. C++20 improved this by allowing try-catch blocks in constexpr functions and ignoring these exception-handling structures in constant-expression contexts. If an exception is thrown during constant-expression evaluation, the compiler reports an error.

The main purpose of this change is to let containers in the standard library support constexpr — std::array for example — while laying the groundwork for more compile-time exception handling in the future.

Example:

constexpr int SafeDivide(int a, int b) {
    if (b == 0) {
        throw std::runtime_error("Division by zero!");  // 当除数为0时抛出异常
    }
    return a / b;
}

constexpr int ComputeQuotient(int x) {
    try {
        return SafeDivide(100, x);  // 尝试除法运算
    } catch (const std::runtime_error&) {
        return -1;  // 捕获除零异常并返回 -1
    }
}

The code above does not compile before C++20. So what does C++20 make of this behavior? Try to guess the outcomes of these two:

static_assert(ComputeQuotient(25) == 4); 
static_assert(ComputeQuotient(0) == -1); 

2. Deprecation of the Comma Operator in Subscripts

Another change in C++20 is deprecating the use of the comma operator inside subscripts. In earlier C++ versions, in the expression array[x, y] the comma operator makes y the subscript passed to the array or container, while x is ignored. This usage, however, makes code hard to understand and gets in the way of future multidimensional arrays. Therefore, as of C++20, the comma operator in subscripts is deprecated, unless parentheses are used to spell out the precedence explicitly, as in array[(x, y)].

Sample code:

// arr[i, j] = 10;  // C++17 中合法,C++20 中弃用
arr[(i, j)] = 10;  // 使用括号以明确逗号的优先级

In this example, arr[(i, j)] uses parentheses to make the precedence of the comma operator explicit, ensuring j is the subscript passed to the array. In C++17, arr[i, j] was allowed, but in C++20 such usage triggers a deprecation warning. Doing it this way keeps the code's behavior consistent and in line with the new C++20 standard.

If you do use the comma operator, the compiler will warn:

common.cc:9:10: warning: top-level comma expression in array subscript is deprecated [-Wcomma-subscript]
    9 |     arr[i, j] = 10;  // C++17 中合法,C++20 中弃用
      |          ^

Summary

These changes in C++20 — the support for exception handling in constexpr functions and the deprecation of the comma operator in subscripts — are all about improving compile-time capabilities and code readability. If your code relies on these old behaviors, it is a good idea to adjust it in time to meet the new C++20 standard.

Comments