【问题标题】:Pointer to portions of array. Operations with filtered array指向数组部分的指针。过滤数组的操作
【发布时间】:2021-10-25 13:12:41
【问题描述】:

根据Pointer to portions of array 中的问题,提出了一种对数组部分进行操作的结构。

但是,我在调用更改矩阵中的条目的函数时遇到了问题。

为矩阵定义运算时,通过引用传递是标准的。但是,当我对过滤后的数组使用相同的结构时,我得到:

错误:无法将“subMat&”类型的非常量左值引用绑定到 “subMat”类型的右值

代码如下:

#include <vector>
#include <array>
#include <iostream>

// define matrix 4x4
typedef std::array<double, 16> matrix4;

// define matrix 3x3
typedef std::array<double, 9>  matrix3;

struct subMat{

    matrix4& matrix_;

    const double& operator[](size_t index) const
    {
        static size_t mapping[] = {0, 1, 2, 4, 5, 6, 8, 9, 10};
        return matrix_[mapping[index]];
    }
    double& operator[](size_t index)
{
    static size_t mapping[] = {0, 1, 2, 4, 5, 6, 8, 9, 10};
    return matrix_[mapping[index]];
}


    subMat (matrix4& A): matrix_(A){}
};

template <typename T>
double sum_of_elements(const T& arr)
{
    double res = 0;
    for (int i=0;i <9; ++i) res += arr[i];
    return res;
}

template <typename T>
void test(T& arr)
{
    for (int i=0;i <9; ++i)
    {
        arr[i] = 1;
    } 
}


int main(int argCount, char *args[])
{
    std::vector<matrix4> myBlockMatrix(5);

    for (int i=0; i < myBlockMatrix.size(); i++)
    {
        for (int j = 0; j<myBlockMatrix[0].size(); j++)
        {
            myBlockMatrix[i][j] = i*j;
        }
    }

    for (int i = 0; i<myBlockMatrix.size(); i++)
    {
        std::cout << sum_of_elements(subMat(myBlockMatrix[i])) << std::endl; // this owrks
    }

    test(subMat(myBlockMatrix[0])); // this does not work

    subMat a = subMat(myBlockMatrix[0]); 
    test(a); // this works and changes the values in myBlockMatrix

    return 0;
}

问题是:我可以用这个结构做什么来重用matrix3 的先前函数。我是否需要始终创建 subMat 的新实例来执行操作?或者有没有办法在函数参数中使用subMat(myBlockMatrix[0])

【问题讨论】:

  • 除非您这样做是为了学习,否则请查看std::val_array&lt;T&gt;,它提供的远不止您所做的。
  • 感谢您的参考。暂时,这是学习C++

标签: c++ c++11


【解决方案1】:

线索在错误消息中:您的 test 函数采用 l 值引用,但 临时 subMat 对象构造为 @987654324 中的参数@call 是一个r值。

最简单的解决方法是重新声明 test 函数模板以具有 forwarding reference 参数:

template <typename T>
void test(T&& arr) // forwarding reference
{
    for (int i = 0; i < 9; ++i) {
        arr[i] = 1;
    }
}

这将允许编译器创建采用左值右值引用的test版本,后者用于参数是临时对象的情况。

从链接的 cppreference 页面:

右值引用可用于延长临时对象的生命周期 对象(注意,对 const 的左值引用可以延长 临时对象也是如此,但它们不能通过它们进行修改)。

【讨论】:

  • 这是一种解决方案,但它不允许我为matrix3 重复使用我的函数。我将不得不重新定义函数以在参数中采用右值
  • @ManuelOliveira 不确定你的意思。 (1) test 被声明为函数模板。 (2) 在你的代码中我看不到任何使用matrix3 类的东西。
  • 简而言之,我将不得不更改函数参数,这是不可取的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
相关资源
最近更新 更多