本文介绍了如何用可变参数函数覆盖 C++ 类中的运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++ 新手这里:我想创建一个模板类来创建不同数据类型和 d 维度的张量,其中 d 由形状指定.例如,形状为 (2, 3, 5) 的张量有 3 个维度,包含 24 个元素.我使用一维向量存储所有数据元素,并希望使用形状信息访问元素以查找元素.

C++ newbie here: I want to create a template class to create tensors of different data types and d dimensions, where d is specified by a shape. For example, a tensor with shape (2, 3, 5) has 3 dimensions holding 24 elements. I store all data elements using a 1d vector and want to access elements using the shape information to find the elements.

我想覆盖 () 操作符来访问元素.由于维度可能会有所不同,() 运算符的输入参数数量也会有所不同.从技术上讲,我可以使用向量作为输入参数,但 C++ 似乎也支持可变参数函数.但是,我无法将头环绕.

I would like to overwrite the () operator to access the elements. Since the dimensions can vary, so can the number of input parameters for the () operator. Technically, I can use a vector as input parameter but C++ also seems to support variadic functions. However, I cannot wrap my head around it.

到目前为止我所拥有的:

What I have so far:

#ifndef TENSOR_HPP
#define TENSOR_HPP

#include <vector>
#include <numeric>
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <stdarg.h>


template <typename T> class Tensor {

    private:
        std::vector<T> m_data;
        std::vector<std::size_t> m_shape;
        std::size_t m_size;

    public:
        // Constructors
        Tensor(std::vector<T> data, std::vector<std::size_t> shape);

        // Destructor
        ~Tensor();

        // Access the individual elements
        T& operator()(std::size_t&... d_args);

};


template <typename T> Tensor<T>::Tensor(std::vector<T> data, std::vector<std::size_t> shape) {
    // Calculate number of data values based on shape
    m_size = std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<std::size_t>());
    // Check if calculated number of values match the actual number
    if (data.size() != m_size) {
        throw std::length_error("Tensor shape does not match the number of data values");
    }
    // All good from here
    m_data = data;
    m_shape = shape;
}

template <typename T> T& Tensor<T>::operator() (std::size_t&... d_args) {
    // Return something to avoid warning
    return m_data[0];
};

template <typename T> Tensor<T>::~Tensor() {
    //delete[] m_values;
};


#endif

不,当我执行以下操作时:

No when I do the following:

std::vector<float> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
std::vector<std::size_t> shape = {2, 3, 4};
Tensor<float> tensor(data, shape);

tensor(2,0,3); // <-- What I would like to do

// Possible workaround with vector which I would like to avoid
// std::vector<std::size_t> index = {2,0,3};
// tensor(index);

我收到错误:

tensor2.hpp:27:33: error: expansion pattern ‘std::size_t&’ {aka ‘long unsigned int&’} contains no parameter packs

使用可变参数函数覆盖 () 运算符的正确方法是什么?

What is the correct approach to overwrite the () operator using a variadic function?

推荐答案

通过提供形状"作为模板参数,你可以这样做:

By providing "shape" as template parameter, you might do:

// Helper for folding to specific type
template <std::size_t, typename T> using always_type = T;

// Your Tensor class
template <typename T, std::size_t... Dims>
class MultiArray
{
public:

    explicit MultiArray(std::vector<T> data) : values(std::move(data))
    {
        assert(values.size() == (1 * ... * Dims));
    }

    const T& get(const std::array<std::size_t, sizeof...(Dims)>& indexes) const
    {
        return values[computeIndex(indexes)];
    }
    T& get(const std::array<std::size_t, sizeof...(Dims)>& indexes)
    {
        return values[computeIndex(indexes)];
    }

    const T& get(always_type<Dims, std::size_t>... indexes) const
    {
        return get({{indexes...}});
    }
    T& get(always_type<Dims, std::size_t>... indexes)
    {
        return get({{indexes...}});
    }

    static std::size_t computeIndex(const std::array<std::size_t, sizeof...(Dims)>& indexes)
    {
        constexpr std::array<std::size_t, sizeof...(Dims)> dimensions{{Dims...}};
        size_t index = 0;
        size_t mul = 1;

        for (size_t i = dimensions.size(); i != 0; --i) {
            assert(indexes[i - 1] < dimensions[i - 1]);
            index += indexes[i - 1] * mul;
            mul *= dimensions[i - 1];
        }
        assert(index < (1 * ... * Dims));
        return index;
    }

    static std::array<std::size_t, sizeof...(Dims)> computeIndexes(std::size_t index)
    {
        assert(index < (1 * ... * Dims));

        constexpr std::array<std::size_t, sizeof...(Dims)> dimensions{{Dims...}};
        std::array<std::size_t, sizeof...(Dims)> res;

        std::size_t mul = (1 * ... * Dims);
        for (std::size_t i = 0; i != dimensions.size(); ++i) {
            mul /= dimensions[i];
            res[i] = index / mul;
            assert(res[i] < dimensions[i]);
            index -= res[i] * mul;
        }
        return res;
    }

private:
    std::vector<T> values; // possibly: std::array<T, (1 * ... * Dims)>
};

用法类似于

std::vector<float> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
MultiArray<float, 2, 3, 4> tensor(data);
std::cout << tensor.get(1, 0, 3); // 16

演示

这篇关于如何用可变参数函数覆盖 C++ 类中的运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 16:18