c++ - C++20 的 std::vector 是如何分配 constexpr 的?

标签 c++ stdvector constexpr c++20

鉴于 vector 的当前内容和大小可以在运行时随机确定,并且在调用分配时必须销毁所有内容,那么 C++20 的 assign member function 又如何呢?是 constexpr 即。编译时?

最佳答案

找到我的答案 here 。引用一下,

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.

因此,与必须在编译时确定的 constexpr 变量不同,给定 constexpr 函数的一次调用可以在编译时运行,具体取决于调用的情况(例如 constexpr std::vector),而另一个调用则在运行时运行(例如,非 constexpr std::vector)。

关于c++ - C++20 的 std::vector 是如何分配 constexpr 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63715105/

相关文章:

c++ - 一次性项目的脚手架/构建系统

php如何从exe文件接收多个cout?

c++ - 如何处理 std::find_if() 返回 false?

c++ - std::vector::data() 是否通过 move 保留?

c++ - 是否允许在 constexpr 函数中进行函数指针比较?

c++ - boost 测试不 init_unit_test_suite

c++ - 将多个参数传递给 CreateThread

c++ - 如何检查 std::vector<std::string> 的元素是否以某个子字符串开头?

c++ - 在 C++ 模板参数中不使用 constexpr

c++ - static constexpr 成员的统一初始化