简体   繁体   中英

How is C++20's std::vector assign constexpr?

Given that the current contents and size of a vector can be randomly determined at runtime, and all contents will have to be destroyed upon calling assign, how can C++20's assign member function be constexpr ie. compile-time?

Found my answer here . To quote,

Being a constexpr function does not mean that the function is executed at compile time. It says, that the function has the potential to run at compile time. A constexpr function can also run a runtime. It's often a question of the compiler and the optimisation level if a constexpr functions runs at compile time or runtime.

Ergo, unlike constexpr variables, which must be determinable at compile time, one call of a given constexpr function may run at compile time, dependent on circumstance of calling (eg. a constexpr std::vector) and another at runtime (eg. a non-constexpr std::vector).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM