鉴于 vector 的当前内容和大小可以在运行时随机确定,并且在调用分配时必须销毁所有内容,那么 C++20 的 assign member function 又如何呢?是 constexpr 即。编译时?
最佳答案
找到我的答案 here 。引用一下,
因此,与必须在编译时确定的 constexpr 变量不同,给定 constexpr 函数的一次调用可以在编译时运行,具体取决于调用的情况(例如 constexpr std::vector),而另一个调用则在运行时运行(例如,非 constexpr std::vector)。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.
关于c++ - C++20 的 std::vector 是如何分配 constexpr 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63715105/