我std::array
用作表示在编译时具有固定长度的向量的基础,并希望std::array::size
用作一个函数来禁用和向量constexpr
的叉积计算。1D
2D
当我std::array::size
在非 constexpr 函数中使用时,它将我的向量作为左值参数,我得到一个错误:
main.cpp: In instantiation of ‘VectorType cross(const VectorType&, const VectorType&) [with VectorType = Vector<double, 3>]’:
main.cpp:97:16: required from here
main.cpp:89:62: error: ‘vec1’ is not a constant expression
89 | return cross_dispatch<std::size(vec1), VectorType>::apply(vec1, vec2);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
main.cpp:89:36: note: in template argument for type ‘long unsigned int’
89 | return cross_dispatch<std::size(vec1), VectorType>::apply(vec1, vec2);
|
这是该main
函数的最小工作示例:
#include <array>
#include <iostream>
using namespace std;
template<typename AT, auto D>
class Vector final
:
public std::array<AT, D>
{
public:
using container_type = std::array<AT,D>;
using container_type::container_type;
template<typename ... Args>
constexpr Vector(Args&& ... args)
:
container_type{std::forward<Args>(args)...}
{}
// Delete new operator to prevent undefined behavior for
// std::array*= new Vector; delete array; std::array has
// no virtual destructors.
template<typename ...Args>
void* operator new (size_t, Args...) = delete;
};
using vector = Vector<double, 3>;
template<std::size_t DIM, typename VectorType>
struct cross_dispatch
{
static VectorType apply(VectorType const& v1, VectorType const& v2)
{
static_assert(std::size(v1) < 3, "Cross product not implemented for 2D and 1D vectors.");
static_assert(std::size(v1) > 3, "Cross product not implemented for ND vectors.");
return VectorType();
}
};
template<typename VectorType>
struct cross_dispatch<3, VectorType>
{
static VectorType apply(VectorType const& v1, VectorType const& v2)
{
return VectorType(v1[1]*v2[2] - v1[2]*v2[1],
v1[2]*v2[0] - v1[0]*v2[2],
v1[0]*v2[1] - v1[1]*v2[0]);
}
};
template <typename VectorType>
VectorType cross(VectorType const& vec1, VectorType const& vec2)
{
return cross_dispatch<std::size(vec1), VectorType>::apply(vec1, vec2);
}
int main()
{
vector p1 {1.,2.,3.};
vector q1 {1.,2.,3.};
cross(p1,q1);
}
我发现这个问题提到了 GCC 8.0 中的一个错误,但我使用的是g++ (GCC) 10.1.0
.
引用答案
表达式 e 是核心常量表达式,除非按照抽象机 (6.8.1) 的规则对 e 的求值将求值以下表达式之一:
... 一个 id 表达式,它引用引用类型的变量或数据成员,除非该引用具有前面的初始化,并且它是用常量表达式初始化的,或者它的生命周期在 e 的计算中开始
这是否意味着,在人类(非标准)语言中,在我的表达式e:=cross(p1,p2)
中,p1
并且p2
在as 之前没有初始化,constexpr
并且它们的生命周期不是以 开头e
,所以即使p1
andp2
是在编译时知道其大小的数据类型的对象 nad谁的 mfunctionsize
是constexpr
mfunction,我现在必须将它们声明为 constexpr,然后才能将它们绑定为不是的函数中的左值constexpr
?