C++11 模板,确定返回类型
我正在构建一个矩阵库,并且尝试使用 policy-基于设计。 所以我的基类是提供存储方法和一些 访问功能。 我还有一个提供数学函数的函数矩阵。 这很好用,但是操作员有一个主要问题* 因为返回类型。我将用一些代码来解释它。
提供堆栈存储的基类:
template < typename T, unsigned int rows, unsigned int cols>
class denseStackMatrix {
public:
typedef T value_type;
private:
value_type grid[rows][cols];
const unsigned int rowSize;
const unsigned int colSize;
然后我有一个提供数学功能的矩阵类:
template <typename MatrixContainer >
class matrix : public MatrixContainer {
public:
typedef MatrixContainer Mcontainer;
matrix<Mcontainer>& operator +(const matrix<Mcontainer>&);
matrix<Mcontainer>& operator *(const matrix<Mcontainer>&);
operator+
始终有效,operator*
仅适用于方阵。 所以我们仍然需要一个适用于所有矩阵的矩阵。事情就是这样 错误的。我已经尝试过一些事情,但没有任何作用。 我在 c++0x 的帮助下寻找类似的东西(使用 c++0x 不是必需的) 你会注意到“???” :)
friend auto operator * (const matrix<T1>& matrix1, const matrix<T2>& matrix2)
-> decltype(matrix<???>);
问题示例
matrix<denseStackMatrix<int,3,2> > matrix1;
matrix<denseStackMatrix<int,2,4> > matrix2;
matrix<denseStackMatrix<int,3,4> > matrix3 = matrix1 * matrix2;
这里它会抱怨类型,因为它与两个参数类型中的任何一个都不匹配。但编译器需要在编译时知道类型,但我不知道如何提供它。
我知道设计还有其他选择,但我真的正在寻找适合这种情况的解决方案..
谢谢!
I am building a matrix library and I am trying to use the policy-based design.
So my base classes are classes that provide a storage method and some
access functions.
I also have a function matrix which provides the mathematical functions.
This works great, but there is a major problem with the operator*
because of the return type. I will explain it with some code.
Base class that provides a stack storage :
template < typename T, unsigned int rows, unsigned int cols>
class denseStackMatrix {
public:
typedef T value_type;
private:
value_type grid[rows][cols];
const unsigned int rowSize;
const unsigned int colSize;
Then I have my matrix class which provides mathematical functionality :
template <typename MatrixContainer >
class matrix : public MatrixContainer {
public:
typedef MatrixContainer Mcontainer;
matrix<Mcontainer>& operator +(const matrix<Mcontainer>&);
matrix<Mcontainer>& operator *(const matrix<Mcontainer>&);
operator+
always works, operator*
only works for square matrix.
So we still need one for all matrices. And that's were it goes
wrong. I have already tried few things, but nothings works.
I look for something like this, with the help of c++0x (usage of
c++0x is not a requirement)
you shall notice the "???" :)
friend auto operator * (const matrix<T1>& matrix1, const matrix<T2>& matrix2)
-> decltype(matrix<???>);
An example of the problem
matrix<denseStackMatrix<int,3,2> > matrix1;
matrix<denseStackMatrix<int,2,4> > matrix2;
matrix<denseStackMatrix<int,3,4> > matrix3 = matrix1 * matrix2;
Here it will complain about the type, because it does not match any of the two parameter types. But the compiler needs to know the type at compile-time and I do not know how to provide it.
I know there are other options for the design, but I am really looking for a solution for this scenario..
Thank you !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
采用@hammar的想法,但具有部分专业化,以允许像问题所示的正常语法:
编辑:正如您在评论中所说,您还可以使用它来创建一个不存在的矩阵不使用将行和列大小作为模板参数的 MatrixContainer:
用法:
Picking up on the idea of @hammar, but with partial specialization to allow the normal syntax like the question shows:
Edit: As you said in your comment, you can also use this to create a matrix that doesn't use a MatrixContainer which has row and column size as template parameters:
Usage:
将
MatrixContainer
更改为模板模板参数怎么样?这样,您不仅可以进行编译时维度检查,还可以扩展它以允许不同容器类型的矩阵之间进行乘法。
How about changing
MatrixContainer
to be a template template parameter?This way you not only get compile time dimensions checking, but you can also extend this to allow multiplications between matrices of different container types.
只是因为我在此处找到所有答案之前对其进行了研究:
此解决方案可能不适合您的设计模式,但使用
operator*
的自由函数实现来推断(并检查)模板参数,从而导致如果不满足矩阵乘法的约束,则会出现编译时错误。Just because I worked on it before finding all the answers here:
This solution may not fit your design pattern, but uses a free function implementation of
operator*
to infer (and check) the template arguments, resulting in a compile-time error if the constraints of matrix multiply are not met.只是一个随机的想法,如果您在基类中包含一种获取相同类型但不同大小的容器的方法会怎样? 说一句
顺便
,我不确定我是否得到了 MatrixContainer::resize 的范围……
我的 2c
just a random idea, what if you include in you base class a way to get a container of the same type but of different size ? something on the lines of:
and then you can do
btw, I'm not sure I got the scoping of MatrixContainer::resize right ...
my 2c
在您的帖子中,我读到您想使用基于策略的设计。在这种情况下,您首先需要定义策略类。因此,您首先需要决定您希望用户可以自己提供哪些类。你参加哪些课程,取决于你。您可以将策略存储和形状用于实例。
您可以制作类似的内容
,如您所见,您现在还可以轻松添加两种不同的矩阵类型。你只需要重载通用模板函数即可。使用策略的一个优点是,您的用户现在可以轻松地提供自己的存储设施。
最后一点。正如您可以使用 C++0x 一样,您还可以为您的用户创建一些快捷方式。例如你可以做类似的事情
In your post I read you would like to use policy-based design. In that case you first need to define your policy classes. Thus, you first need to decide which classes you want that your users can provide by their own. Which classes you take, is up to you. You can for use for instances the policies Storage and Shape.
You can make something like
As you can see, you can also easily add two different Matrix types now. You just need to overload the general template function. An advantage of using policies is that your users can now for instance easily provide their own storage facilities.
One last note. As you can use C++0x you can also make some shortcuts for your users. You can do for instance things like