C++11:函数模板返回任意类型的二维容器
C++11: Function template returning 2D containers of arbitrary types
我正在尝试解决一个大学问题,除其他外,它要求我们编写一个函数,该函数接受一个参数:Type1 的容器,其中包含 Type2 的容器,其中包含 Type3 的容器,其中包含任意类型的元素。容器类型不必不同,但已知的是它们仅支持少数函数(例如 size()),并且所有维度都是相同的。到目前为止,我们只处理了序列容器,所以我想他们会用自定义序列容器对此进行测试。
我需要的是 return 一个包含 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;
}
希望 decltype 能帮助我制作一个包含 Type1 容器的 Type2 容器。但是,当我使用 3D 矢量作为参数调用该函数时:
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++的类型了解甚少,所以在这里我不太明白"deduced"是什么类型。我们几乎没有涉及 decltype 和函数模板的基础知识,而且我们所涉及的其他任何东西都没有想到。我们不允许在我们的课程之外使用太多内容(仅限 C++11),所以我认为要么我在某个地方犯了新手错误,要么有更优雅的方法。
编辑:return 值和平方应该指定为
auto square(remove_reference<decltype(bar[0])> {});
remove_reference<decltype(bar)>
没有从 decltype(bar)
中删除引用 - 你需要
typename remove_reference<decltype(bar)>::type
我正在尝试解决一个大学问题,除其他外,它要求我们编写一个函数,该函数接受一个参数:Type1 的容器,其中包含 Type2 的容器,其中包含 Type3 的容器,其中包含任意类型的元素。容器类型不必不同,但已知的是它们仅支持少数函数(例如 size()),并且所有维度都是相同的。到目前为止,我们只处理了序列容器,所以我想他们会用自定义序列容器对此进行测试。
我需要的是 return 一个包含 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;
}
希望 decltype 能帮助我制作一个包含 Type1 容器的 Type2 容器。但是,当我使用 3D 矢量作为参数调用该函数时:
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++的类型了解甚少,所以在这里我不太明白"deduced"是什么类型。我们几乎没有涉及 decltype 和函数模板的基础知识,而且我们所涉及的其他任何东西都没有想到。我们不允许在我们的课程之外使用太多内容(仅限 C++11),所以我认为要么我在某个地方犯了新手错误,要么有更优雅的方法。
编辑:return 值和平方应该指定为
auto square(remove_reference<decltype(bar[0])> {});
remove_reference<decltype(bar)>
没有从 decltype(bar)
中删除引用 - 你需要
typename remove_reference<decltype(bar)>::type