为什么std :: array :: begin()自C ++ 17起使用constexpr?

问题描述 投票:0回答:1

从C ++ 17开始,std::array<T,N>::begin()是constexpr:

std::array<T,N>::begin()

但是如何在编译时知道constexpr iterator begin() noexcept; 的返回值?例如:

begin

是完全合法的代码(尽管可能有点用处)。基础数组的开始,因此迭代器取决于malloc的地址。

我有一种误解,认为int main() { auto p = std::make_unique<std::array<int,2>>(); auto it = p->begin(); } 会做什么,因为我看不到any非静态成员函数可能是constexpr,特别是如果它(以传递方式)访问数据时成员。

c++ c++17 constexpr compile-time
1个回答
0
投票

constexpr函数可以在非编译时常数表达式中调用。此类调用在运行时进行评估。仅当在编译时常数表达式中调用constexpr函数时,该函数才在编译时求值。

© www.soinside.com 2019 - 2024. All rights reserved.