1

テンプレート化されたクラスを作成しましたが、正常に動作します:

(私はopenCVライブラリを使用しているので、cv::Matタイプの行列があります)

template < class T, T V >
class MatFactory
{

  private:

    const T static VALUE = V;

  public:

    void create(/* some args */)
    {
      cv::Mat M;

      // filling the matrice with values V of type T

      // loop i,j
      M.at<T>(i,j) = V;

      return M;
    }
};

しかし、コードの後半では、いくつかのインデックス (i,j) で行列 M の要素を取得する必要があります。しかし、どうすればタイプ T を知ることができますか?

MatFactory<int, 1> MF;

// getting object
cv::Mat m = MF.create();

// then I need to get some value (with `.at<>` method)
int x = m.at<int>(2,3);

// But, it means I should every time explicitly declare INT type
// Can I somehow get the type, that was passed to template factory
// Than i'll write something like this:
T x = m.at<T>(2,3);

// How to get T from the deferred template?
4

2 に答える 2

1

typeにメンバーを追加するだけMatFactoryです:

template <typename T, T V>
class MatFactory {
public:
    typedef T type;
   ...
};

非型テンプレート引数はかなり制限されていることに注意してください。特に、浮動小数点型は許可されていません (考えてみると、これは C++2011 で変更されている可能性があります)。

于 2012-05-09T22:51:35.867 に答える
1

配列要素の型 (メソッド Mat::type() を使用して取得できる) がわかっている場合は、2 次元配列の要素 M_{ij} に次のようにアクセスできます。

M.at<double>(i,j) += 1.f;

M が倍精度浮動小数点配列であると仮定します。さまざまな次元数に対して、メソッド at にはいくつかのバリエーションがあります。

http://docs.opencv.org/modules/core/doc/basic_structures.html#mat

于 2012-05-09T23:08:12.697 に答える