c++ - 非类型模板参数的用户定义推导指南

标签 c++ templates deduction-guide

我有一个矩阵类的开始。这是代码-

template<int h, int w = h>
class mat {
public:
    mat() : values(h, std::vector<double>(w)) {
        if (w == h) {
            int x = 0;
            for (int y = 0; y < h; y++) {
                values[y][x] = 1;
                x++;
            }
        }
    }
    mat(std::initializer_list<std::vector<double>> matvals){
        values = matvals;
    }
    mat(int val) : values(h, std::vector<double>(w, val)) {}
    mat(const mat& m) {
        values = m.values;
    }
    mat(mat&& m) {
        values = std::move(m.values);
    }
    int width() {
        return w;
    }
    int height() {
        return h;
    }
    template<int mh, int mw = mh>
    auto operator*(const mat<mh, mw>& m) -> mat<h, mw> const {
        if (w != mh) throw std::logic_error{ "Matrices cannot be multiplied" };
        mat<h, mw> temp;
        std::vector<double> mcol(mh);
        for (int y = 0; y < mw; y++) {
            for (int mx = 0; mx < mw; mx++) {
                for (int my = 0; my < mh; my++) {
                    mcol[my] = m.values[my][mx];
                }
                temp.values[y % h][mx] = dot(values[y % h], mcol);
            }
        }
        return temp;
    }
    mat operator+(const mat& m) const {
        mat temp;
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                temp.values[y][x] = values[y][x] + m.values[y][x];
            }
        }
        return temp;
    }
    std::vector<double>& operator[](int y) {
        return values[y];
    }
    mat& operator=(const mat& m) {
        values = m.values;
        return *this;
    }
    mat& operator=(mat&& m) {
        values = std::move(m.values);
        return *this;
    }
private:
    std::vector<std::vector<double>> values;

    template<int mw, int mh>
    friend class mat;
};
到目前为止,这是这个类的使用方式-
mat<2, 4> mat1 = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
};

mat<4, 3> mat2 = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9},
    {10, 11, 12}
};

auto mat3 = mat1 * mat2;
注意到冗余了吗?如果用户想用 std::initializer_list 创建矩阵构造函数,那么他们必须首先在模板参数中指定宽度和高度。此外,还有一个问题是他们是否使用 std::initializer_list如果维度与模板参数中指定的维度不同,则行为将是未定义的。如何编写非类型模板参数的推导指南?我知道如何使用基本模板来完成它,但是我像您一样尝试做的所有事情通常都会出现许多编译器错误。这是所需的行为-
mat mat1 = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
};

mat mat2 = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9},
    {10, 11, 12}
};

auto mat3 = mat1 * mat2;
编辑:
任何真正想要制作矩阵类的人都不应该制作宽度和高度模板参数。它只是不必要地使事情复杂化。

最佳答案

如果您提供这样的构造函数:

template<int w, int h>
struct A{
    A(double (&&c)[w][h]); // `double const (&c)[w][h]` is also OK.
};
无需任何其他扣除指南,您可以按如下方式使用:
A a{{
    {1, 2, 3},
    {4, 5, 6}
}};
A b = {{
    {1, 2, 3},
    {4, 5, 6}
}};
A c({
    {1, 2, 3},
    {4, 5, 6}
});
但是我们可能会觉得外面的大括号很糟糕,所以我们必须提供一个特殊的构造函数和推导指南:
namespace Impl{
    template<typename T, typename = void>
    struct helper;
    template<int h1, int... hs>
    struct helper<std::integer_sequence<int, h1, hs...>, std::enable_if_t<((h1 == hs) && ...)>>{
        static constexpr int w = sizeof...(hs) + 1;
        static constexpr int h = h1;
    };
    template<int... hs>
    inline constexpr int helper_w = helper<std::integer_sequence<int, hs...>>::w;
    template<int... hs>
    inline constexpr int helper_h = helper<std::integer_sequence<int, hs...>>::h;
}

template<int w, int h>
struct A{
    template<int... hs, typename = std::enable_if_t<w + 1 == Impl::helper_w<h, hs...>>>
    A(double (&&... head)[hs]);
};

template<int... hs>
A(double (&&... head)[hs]) -> A<Impl::helper_w<hs...>, Impl::helper_h<hs...>>;
然后你可以随意使用它:
A a{
    {1, 2, 3},
    {4, 5, 6}
};
A b = {
    {1, 2, 3},
    {4, 5, 6}
};

关于c++ - 非类型模板参数的用户定义推导指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66593317/

相关文章:

c++ - 无法将 'QJsonObject' 转换为 'int' 作为返回

c++ - 使用 gcov 忽略或排除外部库中的代码

wordpress - 从 Woocommerce 中的单个产品页面中删除产品尺寸

c++ - 如何在 C++ 中比较两个泛型类型?

c++ - 如何在 C++20 中为模板别名创建推导指南?

c++ - 如何在 C++ 中的多个操作系统上播放声音文件?

c++ - 常量的隐式转换

django - urlencode 过滤器在 Django 模板中不起作用

c++ - 'constexpr' 标记的变量和静态存储持续时间的变量是否都可以通过具有推导指南的类类型来存储?

c++ - 如何编写聚合模板别名的推导指南?