为了使函数 return 编译时常量值,模板中是否需要 constexpr const 类型?

is constexpr const Type necessary in template in order for functions to return compile time const values?

查看 std::array 在 c++ 中的实现,我看到一些我无法完全理解的东西..

例如returns数组第一个元素定义为的函数:

constexpr const-reference
front() const noexcept
{.......}

其中 const-reference 定义为 const value_type&,因此上面的整个表达式的计算结果为 constexpr const value_type&。知道在某些情况下我们可能希望在编译时知道函数返回的值,我的问题是为什么他们在同一行上同时使用 constexprconstconst 不是多余的吗,因为已经说过我们将要返回 constexpr

so the whole expression above evaluates to constexpr const value_type&

不完全是:constexpr,对于 function/method,意味着 function/method 可以在编译时执行(也),而不是返回值是 constexpr .

my question is why are they using both constexpr and const on the same line? isn't the const redundant as it was already said that we are going to be returning a constexpr ?

它在 C++11 中是多余的。

constexpr 是在 C++11 中引入的,在 C++11 中,constexpr 方法也(必然)是 const 方法。

这从C++14开始改变了(一个很好的解释here

所以,从 C++14 开始,consexprconst(对于一个方法)是解耦的,正如你所看到的 in cppreferencestd::array::front()const版本是constexpr(从C++17开始)。