0

2 つのクラスから継承するクラスがあります。1 つは自分の基本クラスで、もう 1 つはテンプレート クラスです。

typedef typename cusp::csr_matrix< int,
                                   float,
                                   cusp::host_memory > csr_matrix;

class CuspMatrix
: 
  public csr_matrix,
  public Matrix
{
...
}

ある時点で、次のように基本クラス オブジェクトをホストからデバイスにコピーする割り当てを行う必要があります。

cusp::csr_matrix<int,float,cusp::host_memory> A(4,3,6);
cusp::csr_matrix<int,float,cusp::device_memory> A = B;

しかし、それを行う前に、これをその基本クラスcsr_matrixにアップキャストする必要があります

static_castとカスタムキャスト演算子を試しました:

operator csr_matrix()
{
  return *( cusp::csr_matrix< int,float,cusp::device_memory> *)this;
}

ただし、実際に実行しようとすると、コンパイラから大量のエラーが発生します

cusp::csr_matrix<int,float,cusp::device_memory> mtx = *(csr_matrix *)this;

実際、静的キャストもこの時点では理解できません。

auto me = static_cast<csr_matrix>( *this );
cusp::csr_matrix<int,float,cusp::device_memory> mtx = me;

それでも、typedef を使用しない C スタイルのショットガン キャストは機能するようです。

auto me = *( cusp::csr_matrix< int,
                               float,
                               cusp::host_memory> *)this;

しかし、typedef で失敗します:

auto me = *( csr_matrix *)this;
  • では、できれば静的キャストを使用して、独自のカスタム オペレーターを使用して安全にアップキャストするにはどうすればよいでしょうか?

  • 完全な namespace::type でのキャストが機能するのに、typedef で失敗するのはなぜですか?

4

1 に答える 1

1
cusp::csr_matrix<int,float,cusp::device_memory> mtx = *(csr_matrix *)this;

thisキャスト式のオペランドの型が であるため、このキャストは変換関数を呼び出すことはできませんCuspMatrix*。変換関数は、オペランドの型がクラス型である場合にのみ考慮されます。

cusp::csr_matrix<int,float,cusp::device_memory> mtx = (csr_matrix)*this;

このシナリオでは、csr_matrix は既に公開基底クラスであるCuspMatrixため、変換関数CuspMatrix::operator csr_matrix()を呼び出すことはできません。

この上方変換はキャストを必要としません -thisが型でからの代入CuspMatrix*をサポートしている場合、これを行うことができるはずです:cusp::csr_matrix<int,float,cusp::device_memory>cusp::csr_matrix<int,float,cusp::host_memory>

cusp::csr_matrix<int,float,cusp::device_memory> mtx = *this;

実際のエラー メッセージとコンパイル可能な例を見ないと、2 番目の質問に答えることは困難です。

于 2013-07-12T15:56:20.000 に答える