我可以使用 const & 作为 constexpr 函数的参数吗?

Can I use const & as parameter of a constexpr function?

我正在编写一个接受 CArray T(&)(N)std::array 的 constrexpr 函数。 我想我必须写 2 个函数(如果你更了解我会很高兴知道),

但是我担心我用 std::array

写的东西
    constexpr float LinInterp01(const std::array<float, N>& inArray, float inX);

写constrexpr函数时传const & or not是否正确?

我想应该是因为在编译时编译器会实例化一个副本,并且在编译时没有L值的概念。

谁能给我解释一下?

C++ 标准部分 § 7.1.5 [dcl.constexpr]

The definition of a constexpr function shall satisfy the following constraints:

— it shall not be virtual (10.3);

— its return type shall be a literal type;

— each of its parameter types shall be a literal type;

以及第 3.9 节 [basic.types]

A type is a literal type if it is:

— void; or

— a scalar type; or

a reference type; or

— an array of literal type; or

— a class type (Clause 9) that has all of the following properties:

— it has a trivial destructor,

— it is an aggregate type (8.5.1) or has at least one constexpr constructor or constructor template that is not a copy or move constructor, and

— all of its non-static data members and base classes are of non-volatile literal types.

所以,可以通过引用传递参数给constexpr函数。

现在,您的函数调用是否会在编译时实际求值取决于 LinInterp01 的主体和调用。