2017-04-23 11 views
0

私は大学の問題を解決しようとしています。とりわけ、パラメータを取る関数を書く必要があります。コンテナを含むType1のコンテナType2のType3のコンテナを含み、任意の型の要素を含む。コンテナの種類は異なる必要はありませんが、いくつかの関数(size()など)のみをサポートし、すべての次元が同じであることがわかります。ここまではシーケンスコンテナしか扱っていなかったので、カスタムシーケンスコンテナでこれをテストしていると推測します。C++ 11:任意の型の2Dコンテナを返す関数テンプレート

私が必要とするのは、Type1コンテナを含むType2コンテナを返す関数です。 decltypeは私がType1のコンテナが含まれているタイプ2のコンテナを作る助けになることを期待して

template <typename Cube> 
    auto foo(const Cube &bar) -> remove_reference<decltype(bar)> 
    { 
     auto square(remove_reference<decltype(bar)> {}); 
     cout << square.size(); // Throws an error 
     return square; 
    } 

:私はこのような工事を使用しようとしました。私は、パラメータとして3次元ベクトルで関数を呼び出すときしかし、:

int main() { 
    vector<vector<vector<int>>> v {{{1}}}; 
    foo(v); 
} 

これは、エラーがスローされます:

error: no member named 'size' in 'std::remove_reference<const std::vector<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >, std::allocator<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > > > &>' 
       cout << square.size(); 
         ~~~~~~^ 
note: in instantiation of function template specialization 'foo<std::vector<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >, std::allocator<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > > > >' requested here 
    foo(v); 
    ^

私はC++の型の貧弱な知識を持っているので、私は本当に何を理解していませんタイプはここで「推論」されます。私たちは、decltypeや関数テンプレートを取り巻く環境はほとんどカバーしていませんでした。私たちは私たちのカリキュラム(C++ 11のみ)の外でそれほど多くのものを使用することは許されていないので、私はどこかでルーキーミスを犯したか、これを行うもっとエレガントな方法があると推測します。

EDIT:戻り値と広場

auto square(remove_reference<decltype(bar[0])> {}); 

答えて

0

remove_reference<decltype(bar)>として指定されているべきであるがdecltype(bar)から参照を削除されていません - あなたはremove_reference documentationを読む

typename remove_reference<decltype(bar)>::type 

を必要としています。

関連する問題