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

时间:2022-03-05 18:52:04

I'm trying to solve a college problem, that, among other things, requires us to write a function which takes a parameter: container of Type1, that contains containers of Type2, which contain containers of Type3, which contain elements of an arbitrary type. The container types do not have to be different, but all that is known is that they support only a few functions (e.g. size( )), and that all the dimensions are the same. We've only dealt with sequence containers so far, so I presume they'll be testing this with custom sequence containers.

我正在尝试解决大学问题,除其他外,要求我们编写一个函数,它接受一个参数:Type1的容器,它包含Type2的容器,它包含Type3的容器,包含任意类型的元素。容器类型不必相同,但所有已知的是它们仅支持少数函数(例如size()),并且所有维度都相同。到目前为止,我们只处理了序列容器,因此我认为他们将使用自定义序列容器对其进行测试。

What I need is a function that returns a Type2 container that contains Type1 containers. I tried to use a construction like this:

我需要的是一个函数,它返回一个包含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;
    }

Hoping that decltype would help me make a Type2 container that contains Type1 containers. However, when i call the function with a 3D vector as a parameter:

希望decltype可以帮助我创建一个包含Type1容器的Type2容器。但是,当我用3D矢量作为参数调用函数时:

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

It throws an error:

它抛出一个错误:

    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);
    ^

I have poor knowledge of C++ types, so I don't really understand what type is "deduced" here. We've covered very little ground surrounding decltype and function templates, and nothing else we've covered that could help comes to mind. We're not allowed to use so much outside our curriculum (C++11 only) so I presume that either I've made a rookie mistake somewhere or there's a more elegant way of doing this.

我对C ++类型知之甚少,所以我真的不明白这里“推断”了什么类型。我们已经涵盖了很少有关于decltype和函数模板的基础,而我们所涵盖的任何其他内容都无法想到。我们不允许在我们的课程之外使用这么多(仅限C ++ 11)所以我认为要么我在某个地方犯了一个菜鸟错误,要么就是这样做更优雅的方式。

EDIT: the return value and square should be have been specified as

编辑:返回值和平方应指定为

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

1 个解决方案

#1


0  

remove_reference<decltype(bar)> is not removing the reference from decltype(bar) - you need

remove_reference 没有从decltype(bar)中删除引用 - 你需要 (bar)>

typename remove_reference<decltype(bar)>::type

Read the remove_reference documentation.

阅读remove_reference文档。

#1


0  

remove_reference<decltype(bar)> is not removing the reference from decltype(bar) - you need

remove_reference 没有从decltype(bar)中删除引用 - 你需要 (bar)>

typename remove_reference<decltype(bar)>::type

Read the remove_reference documentation.

阅读remove_reference文档。