C++11 C++;11:返回任意类型的2D容器的函数模板

C++11 C++;11:返回任意类型的2D容器的函数模板,c++11,containers,decltype,function-templates,C++11,Containers,Decltype,Function Templates,我试图解决一个大学的问题,除其他外,需要我们编写一个函数,它接受一个参数:类型1的容器,它包含类型2的容器,它包含类型3的容器,它包含任意类型的元素。容器类型不必不同,但已知的是它们只支持少数函数(例如size()),并且所有维度都相同。到目前为止,我们只处理了序列容器,所以我认为他们将使用自定义序列容器来测试这一点 我需要的是一个函数,它返回一个包含Type1容器的Type2容器。我试着使用这样的结构: template <typename Cube> auto foo(c

我试图解决一个大学的问题,除其他外,需要我们编写一个函数,它接受一个参数:类型1的容器,它包含类型2的容器,它包含类型3的容器,它包含任意类型的元素。容器类型不必不同,但已知的是它们只支持少数函数(例如size()),并且所有维度都相同。到目前为止,我们只处理了序列容器,所以我认为他们将使用自定义序列容器来测试这一点

我需要的是一个函数,它返回一个包含Type1容器的Type2容器。我试着使用这样的结构:

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;
    }
模板
自动foo(常量立方体和条形)->删除\u引用
{
自动平方(删除_参考{});
cout
remove\u reference
不是从
decltype(bar)
中删除引用-您需要

typename remove_reference<decltype(bar)>::type
typename删除\u引用::type
读这本书

    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);
    ^
auto square(remove_reference<decltype(bar[0])> {});
typename remove_reference<decltype(bar)>::type