Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::vector::push_back declared as constexpr in C++20?

According to cppref, in C++20, std::vector::push_back is declared as follows:

constexpr void push_back(const T& value);

I cannot imagine a scenario in which push_back should be constexpr.

What's the rationale behind?

like image 990
xmllmx Avatar asked Mar 01 '23 13:03

xmllmx


1 Answers

Is there any rationale behind?

This is the abstract of the proposal. There is no separate rationale section:

P1004R2 Making std::vector constexpr

Abstract

std::vector is not currently constexpr friendly. With the loosening of requirements on constexpr in [P0784R1] and related papers, we can now make std::vector constexpr, and we should in order to support the constexpr reflection effort (and other evident use cases).

like image 157
eerorika Avatar answered Mar 12 '23 08:03

eerorika