2012-02-27 12 views
3

私はdoubleの3D配列を持っています。私は単純な&汎用関数を書いて、2Dスライスを印刷したいと思います。Boost.MultiArrayの2Dビューを引数として関数に取り込む方法は?

コード:

test.cpp: In function ‘int main()’: 
test.cpp:24:79: error: no matching function for call to ‘printFloatMatrix(boost::multi_array_ref<double, 3u>::array_view<2u>::type)’ 
test.cpp:24:79: note: candidate is: 
test.cpp:5:6: note: template<class M> void printFloatMatrix(typename M::array_view<2u>::type) 

なぜエラー:GCCで

#include <cstdio> 
#include <boost/multi_array.hpp> 

template<class M> // any model of MultiArray concept 
void printFloatMatrix(typename M::template array_view<2u>::type matrix) { 
    using std::printf; 

    for(auto& row : matrix) { 
     for(auto& elem : row) { 
      printf("%5.3f ", elem); 
     } 
     printf("\n"); 
    } 
} 


int main() { 
    typedef boost::multi_array<double,3> data_t; 
    data_t test_matrix{data_t::extent_gen()[10][10][2]}; 
    // ... 

    using boost::indices; 
    using boost::multi_array_types::index_range; 
    printFloatMatrix(test_matrix[ indices[index_range()] [index_range()] [0] ]); 
} 

これは、エラーメッセージが表示されますか?

なぜMboost::multi_array_ref<double, 3u>であると推定されますか?

どのように動作するプロトタイプを作成しますか?

+0

[テンプレートとタイプ名]のキーワードをどこに入れなければならないのですか?(http://stackoverflow.com/questions/610245/where-and-why-do-i-haveテンプレート名と型名のキーワード) – Xeo

答えて

1

私はここでC++型推論が失敗する正確な理由を綴ることはできませんが、関数プロトタイプをtemplate<class M> void printFloatMatrix(const M& matrix)に変更したのはなぜですか?

今、試作品は無駄に広いです。高いチャンスで、将来私を噛んでしまうでしょう。このような状況は、コンセプトの出現によって修正されることが望ましく、静的なアサーションを用いて回避することもできます。

Freenodeの##c++に感謝します。

+4

'typename X :: Y'があるときはいつでも、あなたはいわゆる「推論不可能なコンテキスト」を持っています。つまり、テンプレート引数はそのパラメータの引数を渡します。コールサイトでタイプを指定するか、ここで行ったのと同じようにその非deducibleコンテキストを変更するか、テンプレート引数の減算を可能にする別のパラメータを指定する必要があります。 – Xeo

+0

これは、テンプレートのチューリング完全性と停止問題に関連している必要があります。ありがとう、今私は理解する。 – ulidtko

関連する問題