为什么这次闪电战不发生? 代码编译?

发布于 2024-07-17 00:38:06 字数 1025 浏览 3 评论 0原文

我是 blitz++ 新手。 到目前为止,一切都很好,但我有点困惑为什么下面代码中的注释行无法

error: conversion from ‘blitz::_bz_tinyMatExpr<blitz::_bz_tinyMatrixMatrixProduct<double, double, 3, 3, 3, 3, 1, 3, 1> >’ to non-scalar type ‘const m33’ requested

在 Debian/Lenny(g++ 4.3.2,Blitz 0.9)上编译。 这是代码:

#include <blitz/blitz.h>
#include <blitz/array.h>
#include <blitz/matrix.h>
#include <blitz/matexpr.h>
#include <blitz/tinymat.h>
#include <blitz/tinymatexpr.h>
#include <blitz/tinyvec.h>
#include <blitz/tinyvec-et.h>
#include <blitz/vector.h>
#include <blitz/vector-et.h>

typedef blitz::TinyVector<double,3> v3;
typedef blitz::TinyMatrix<double,3,3> m33;

int main(int,char**)
{
  const m33 a;
  const m33 b;
  m33 c;c=blitz::product(a,b);  // Compiles OK
  //const m33 d=blitz::product(a,b);  // Fails to compile.
  return 0;
}

我确实喜欢完全 const-ed,所以很高兴知道是否有办法让它工作(到目前为止 Blitz++ 的经验表明这可能只是选择正确的问题)包括...)。

I'm a blitz++ newbie. So far, so good, but I'm a bit mystified why the commented out line in the code below fails to compile with

error: conversion from ‘blitz::_bz_tinyMatExpr<blitz::_bz_tinyMatrixMatrixProduct<double, double, 3, 3, 3, 3, 1, 3, 1> >’ to non-scalar type ‘const m33’ requested

I'm on Debian/Lenny (g++ 4.3.2, Blitz 0.9).
Here's the code:

#include <blitz/blitz.h>
#include <blitz/array.h>
#include <blitz/matrix.h>
#include <blitz/matexpr.h>
#include <blitz/tinymat.h>
#include <blitz/tinymatexpr.h>
#include <blitz/tinyvec.h>
#include <blitz/tinyvec-et.h>
#include <blitz/vector.h>
#include <blitz/vector-et.h>

typedef blitz::TinyVector<double,3> v3;
typedef blitz::TinyMatrix<double,3,3> m33;

int main(int,char**)
{
  const m33 a;
  const m33 b;
  m33 c;c=blitz::product(a,b);  // Compiles OK
  //const m33 d=blitz::product(a,b);  // Fails to compile.
  return 0;
}

I do like to be const-ed to the hilt, so it'd be nice to know if there's a way of getting it to work (experience with Blitz++ so far suggests it might just be a matter of picking the right includes...).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨轻弹 2024-07-24 00:38:06

我看过 Blitz++ 的源代码。
看起来令人惊讶的是,TinyMatrix 没有模板构造函数,但有一个 template = 运算符。
这意味着你无法做你想做的事。 所以我建议忘记你的矩阵是 const 。 或者找到另一种方法,例如创建一个非常量矩阵,将其作为常量引用作为函数的参数传递。

现在,只是为了好玩,事实是您可以复制模板operator=() 代码并在blitz/tinymat.h 中创建一个新的模板构造函数,但我不会! 如果您真的想要,请按照以下方式进行:

// Turn the following code...

template<typename T_expr>
TinyMatrix<T_numtype, N_rows, N_columns>&
operator=(_bz_tinyMatExpr<T_expr> expr)
{
    // USEFUL CODE
    return *this;
}

// ...into this :
template<typename T_expr>
TinyMatrix(_bz_tinyMatExpr<T_expr> expr)
{
    // USEFUL CODE
}

以及您的代码的编译(并且可能有效)。

I have had a look at the source code of Blitz++.
As surprising as it may seem, there is no template constructor for TinyMatrix, but there is a template = operator.
That means that you cannot do what you are trying to do. So I'd suggest forgetting about your matrix being const. Or find another way like creating a non-const matrix that you would pass as a const reference as the parameter of a function.

Now, just for fun, the truth is you could copy template operator=() code and make a new template constructor inside blitz/tinymat.h out of it, but i wouldn't ! If you really want to, here's how :

// Turn the following code...

template<typename T_expr>
TinyMatrix<T_numtype, N_rows, N_columns>&
operator=(_bz_tinyMatExpr<T_expr> expr)
{
    // USEFUL CODE
    return *this;
}

// ...into this :
template<typename T_expr>
TinyMatrix(_bz_tinyMatExpr<T_expr> expr)
{
    // USEFUL CODE
}

and your code compiles (and probably works).

所有深爱都是秘密 2024-07-24 00:38:06
//const m33 d=blitz::product(a,b);  // Fails to compile.
const m33 d;
const_cast<m33>(d) = blitz::product(a,b);
//const m33 d=blitz::product(a,b);  // Fails to compile.
const m33 d;
const_cast<m33>(d) = blitz::product(a,b);
隐诗 2024-07-24 00:38:06

抱歉,我不知道闪电战,但一些想法可能会有所帮助。

blitz::TinyMatrix 是否有合适的构造函数?
也许尝试其他形式的复制构造函数

const m33 d( blitz::product(a,b) );

应用标准 const 关键字真的合适吗? 也许你可能需要

typedef blitz::TinyMatrix<const double,3,3> const_m33;
typedef blitz::ConstTinyMatrix<double,3,3> const_m33;

sorry don't know blitz, but some ideas that might help.

Does the blitz::TinyMatrix have appropriate constructors?
Perhaps try the other form of copy constructor

const m33 d( blitz::product(a,b) );

Is applying the standard const keyword really appropriate? Perhaps you might need

typedef blitz::TinyMatrix<const double,3,3> const_m33;
typedef blitz::ConstTinyMatrix<double,3,3> const_m33;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文