在非constexpr函数中作为左值传递的变量上使用constexpr函数

时间:2020-06-23 13:13:07

标签: c++ constexpr c++20 if-constexpr

我正在使用std::array作为表示在编译时具有固定长度的向量的基础,并且想使用std::array::size作为constexpr的函数来禁用以下项的叉积的计算1D2D个向量。

当我在非constexpr函数中使用std::array::size时,将我的向量作为左值参数,我得到一个错误:

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);
}

我发现this question提到了GCC 8.0中的一个错误,但是我正在使用g++ (GCC) 10.1.0

引用the answer

除非对e求值,否则表达式e是核心常数表达式。 e,遵循抽象机(6.8.1)的规则,将评估 以下表达式之一:

...引用ID的变量或数据成员的id表达式 引用类型,除非引用具有先前的初始化,并且 用常量表达式或它的生存期初始化 始于e

的评估

在人类(非标准)语言中,这是否意味着在我的表达式e:=cross(p1,p2)p1p2中没有被初始化为{ {1}}及其生存期不是constexpr开头,因此即使ep1是数据类型的对象,其大小在编译时就已知现在,其mfunction p2 size mfunction的时候,我现在必须将它们声明为constexpr,然后再将它们绑定为非constexpr的函数中的左值? / p>

1 个答案:

答案 0 :(得分:1)

下面,我回答为什么您的代码不起作用。关注您的用例:正如其他人所说的,std::array::size不是static,所有std::size所做的就是调用该非静态函数。最好的选择是向您的static类中添加一个Vector大小的函数:

static constexpr auto size() {
    return D;
}

您无法实现cross,因为您不能使用非常数表达式来初始化模板。有关为什么函数参数不是常量表达式的信息,请参见this SO answer

基本上,调用cross函数需要为cross_dispatch的每个不同值生成std::size(vec1)结构的新实例,这还需要知道 address vec1调用非静态函数以来,每个给定std::size的值都在编译时。从中应该可以看出,编译器根本不知道需要创建cross_dispatch的哪些实例。

以上,我提供了针对您的用例的解决方案。如果您要做的不仅仅是测量Vector的大小,第二种解决方案将涉及将对象作为模板参数传递(这将要求它们为static):

template <typename VectorType, VectorType const& vec1, VectorType const& vec2>
constexpr VectorType cross()
{
    return cross_dispatch<std::size(vec1), VectorType>::apply(vec1, vec2);  
}

int main()
{
    static vector p1 {1.,2.,3.}; 
    static vector q1 {1.,2.,3.}; 

    cross<vector, p1, q1>();
}

由于p1q1是静态的,因此可以在编译时知道它们的地址,从而可以初始化cross的模板。模板参数在运行时不会更改,因此std::size(vec1)现在是一个常量表达式。