如何使我的向量 class 支持直接 initialization/assignment

How to make my vector class support direct initialization/assignment

我的问题肯定很愚蠢,但我对 C++11 和模板编程的理解有很多漏洞,我不知道如何解决这个问题。

我正在推出我自己的非常简单的线性代数库:

typedef short index_t;

template<int M, int N, typename T = double>
class mat {
    // may want some specialized constructors for mat<1,N,T> and mat<M,1,T>
public:
    T& operator()(index_t i, index_t j) {
        return buf[i + j*M];
    }
    T& operator[](index_t k) { // useful for special cases where matrix is vector
        return buf[k];
    }

    // etc...

private:
    std::array<T, M*N> buf;
}

typedef mat<2, 1, double> col2d;
typedef mat<3, 1, double> col3d;
typedef mat<1, 2, double> row2d;
typedef mat<1, 3, double> row3d;
typedef mat<2, 2, double> mat2d;
typedef mat<3, 3, double> mat3d;

我只是希望它支持一种直接分配(或至少初始化)向量(即具有单一维度的矩阵)的方法。例如,我希望能够做到 v = col2d(v1,v2) 或至少 col2d v = {v1,v2}。我的印象是 buf public 可能允许 col2d v = {{v1, v2}} 但我不喜欢暴露 buf 的想法。我不热衷于为每个 (1,N) 和每个 (M,1) 编写专门的构造函数。我正在努力使库尽可能简单易读。

有什么建议吗?

mat(std::array<T, M*N>&& buffin):buf(std::move(buffin)){}
mat(std::array<T, M*N> const& buffin):buf(buffin){}

这不会公开 buf,但会为您提供 col3d x = {{{ 1,2,3 }}}; 语法。

它还允许使用平面缓冲区初始化 mat 3d。我自己觉得这很有用。

我可能想添加

struct flat_t{constexpr flat_t(){};};
constexpr flat_t flat{};

然后用 flat_tarray 前添加到 mat。这可以防止意外的隐式转换,同时仍然 return {flat, {{1,2,3}}}; 风格 return.

另一个选择是

template<class...Ts,
  std::enavle_if_t< (sizeof...(Ts)==N*M), int> =0
>
mat(flat_t, Ts&&...ts):
  buf{{std::forward<Ts>(ts)....}}
{}

哪个给你

col2d{flat, 1,2};

作为有效 col2d.

添加所有 Ts 都可以转换为 T 的 sfinae 测试是此路径的下一步。

这是我最后做的事情:

template<int M, int N, typename T = double>
class mat {
public:
    // constructor
    template <typename... Args>
    mat(Args... args) : buf({ args... }) {
        static_assert(sizeof...(Args) == M*N || sizeof...(Args)==0, "Wrong number of arguments in constructor.");
    }
    // etc...
}

// the following aliases replace all those typedefs in the question
template<index_t N, typename T=double>
using row = mat<1, N, T>;
template<index_t M, typename T=double>
using col = mat<M, 1, T>;

template <typename T=double, typename... Args>
col<sizeof...(Args), T> c(Args... args) {
    return col<sizeof...(Args), T>({ args... });
}
template <typename T=double, typename... Args>
row<sizeof...(Args), T> r(Args... args) {
    return row<sizeof...(Args), T>({ args... });
}

允许

c(1.0, 2.0, 3.0); // returns a col<3>, which is a mat<3,1>
r(1.0, 2.0);      // returns a row<2>, which is a mat<1,2>
r<float>(1.0f, 2.0f); // returns a row<2, float>

如果上面最后一行中的模板函数有一种简单的方法从函数的参数类型推断类型参数,我很想知道它。


编辑:(新版本结合了@Yakk 的建议。)

typedef short index_t;

template<int M, int N, typename T = double>
class mat {
public:
    mat() {
    }
    template <typename... Args, std::enable_if_t<(sizeof...(Args)==M*N), int> = 0> // static assertion doesn't work for external detection of validity, which is needed I think to avoid signature conflict with default, copy, move constructors etc.
    mat(Args&&... args) : buf({ std::forward<Args>(args)... }) {
        //static_assert(sizeof...(Args) == M*N || sizeof...(Args) == 0, "Wrong number of arguments in constructor.");
    }
public:
    T& operator()(index_t i, index_t j) {
        return buf[i + j*M];
    }
    T& operator[](index_t k) {
        return buf[k];
    }

    // etc... various matrix operations

private:
    std::array<T, M*N> buf;
};

// etc... various matrix operators

// aliases for row and col
template<index_t N, typename T=double>
using row = mat<1, N, T>;
template<index_t M, typename T=double>
using col = mat<M, 1, T>;

// short-hand "literals"
template <typename... Args>
col<sizeof...(Args), typename std::common_type<Args...>::type> c(Args&&... args) {
    return col<sizeof...(Args), typename std::common_type<Args...>::type>({ std::forward<Args>(args)... });
}
template <typename... Args>
row<sizeof...(Args), typename std::common_type<Args...>::type> r(Args&&... args) {
    return row<sizeof...(Args), typename std::common_type<Args...>::type>({ std::forward<Args>(args)... });
}