如何在C ++ 14中定义静态constexpr矩阵? [英] How to define a static constexpr matrix in c++14?
问题描述
我当前正在使用C ++ 14.
我想定义一个Matrix
类,该类可用于定义运行时矩阵,还可以用于定义constexpr矩阵.我也想基于这样的类定义静态constexpr矩阵.
I am currently using C++14.
I would like to define a Matrix
class which I can use for defining runtime matrices, but also constexpr matrices. I also would like to define static constexpr matrices based on such a class.
我考虑此作为Matrix
类的起点.
然后,我想写一些东西:
I consider this as a starting point for the Matrix
class.
Then I would like to write something as:
static constexpr Matrix<double,2,2> staticmat{0.1,0.2,0.3,0.4};
使得staticmat是constexpr并且是唯一的,是静态的.
so that staticmat is constexpr and unique, being static.
但是,为了进行初始化,我需要一个constexpr
数组或一个constexpr
初始化程序列表(在我发布的链接中未实现,但不会有太大变化).所以我可以这样写:
However, in order to initialise this, I would need a constexpr
array or a constexpr
initialiser list (not implemented in the link I posted, but not much would change). So I could write something like:
static constexpr std::array<double,4> staticmattmp{0.1,0.2,0.3,0.4};
static constexpr Matrix<double,2,2> staticmat(staticmattmp);
这将是丑陋的,因为我必须为一个定义两个东西,但是,如果可行,我可以接受.不幸的是,编译器显示unknown type name 'staticmattmp'
.
This would be ugly because I have to define two things just for one, but, if it worked, I could accept it. Unfortunately the compiler says unknown type name 'staticmattmp'
.
我怎么可能以一种优雅的方式解决这个问题?
How can I solve this, maybe in an elegant way?
推荐答案
我怎么可能以一种优雅的方式解决这个问题?
How can I solve this, maybe in an elegant way?
我不知道它是否优雅,但是...做了一点工作...
I don't know if it's elegant but... with a little work...
首先,定义以下using
template <typename T, std::size_t>
using getType = T;
下一次重新声明(仅声明;未定义),如下所示Matrix
Next re-declare (declare only; not define) Matrix
as follows
template <typename, std::size_t NR, std::size_t NC,
typename = std::make_index_sequence<NR*NC>>
class Matrix;
现在将您的Matrix
声明为类部分专业化,添加一个构造函数,该构造函数接收类型为T
的NR*NC
元素,并使用它们初始化内部的std::array
Now declare your Matrix
as a class partial specialization adding a constructor that receive NR*NC
elements of type T
and use them to initialize the internal std::array
template <typename T, std::size_t NR, std::size_t NC, std::size_t ... Is>
class Matrix<T, NR, NC, std::index_sequence<Is...>>
{
public:
using value_type = T;
constexpr Matrix (getType<value_type, Is> ... vals)
: values_{{vals...}}
{}
// other member and methods
};
但是不要忘了将析构函数(可能也是构造函数和operator=()
)声明为default
.
But don't forget to declare as default
the destructor (maybe also constructor and operator=()
).
以下是完整的C ++ 14示例
The following is a full compiling C++14 example
#include <array>
#include <type_traits>
template <typename T, std::size_t>
using getType = T;
template <typename, std::size_t NR, std::size_t NC,
typename = std::make_index_sequence<NR*NC>>
class Matrix;
template <typename T, std::size_t NR, std::size_t NC, std::size_t ... Is>
class Matrix<T, NR, NC, std::index_sequence<Is...>>
{
public:
using value_type = T;
constexpr Matrix (getType<value_type, Is> ... vals)
: values_{{vals...}}
{}
constexpr Matrix (std::array<T, NR*NC> const & a)
: values_{a}
{}
constexpr Matrix (std::array<T, NR*NC> && a)
: values_{std::move(a)}
{}
constexpr Matrix () = default;
~Matrix() = default;
constexpr Matrix (Matrix const &) = default;
constexpr Matrix (Matrix &&) = default;
constexpr Matrix & operator= (Matrix const &) = default;
constexpr Matrix & operator= (Matrix &&) = default;
constexpr T const & operator() (std::size_t r, std::size_t c) const
{ return values_[r*NC+c]; }
T & operator() (std::size_t r, std::size_t c)
{ return values_[r*NC+c]; }
constexpr std::size_t rows () const
{ return NR; }
constexpr std::size_t columns () const
{ return NC; }
private:
std::array<T, NR*NC> values_{};
};
int main()
{
static constexpr Matrix<double,2,2> staticmat{0.1,0.2,0.3,0.4};
}
这篇关于如何在C ++ 14中定义静态constexpr矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!