在下面的片段中,我使用auto获取A.shape()的输出,它实际上返回std::size_t*。然后,我想要创建一个具有相同底层数据类型的数组,即std::size_t。在这种情况下,我很难找到如何使用decltype()。以下几点不起作用:
#include "boost/multi_array.hpp"
int main() {
boost::multi_array<double, 3> A(boost::extents[5][4][2]);
auto dims = A.shape();
boost::array<decltype(*dims), 3> dims3; // does not compile
}但是,如果我创建一个中间变量auto d0 = *dims;,则decltype(d0)的使用是成功的:
#include "boost/multi_array.hpp"
int main() {
boost::multi_array<double, 3> A(boost::extents[5][4][2]);
auto dims = A.shape();
auto d0 = *dims;
boost::array<decltype(d0), 3> dims3; // this works
}有什么更好的办法让这件事成功吗?最好不用生成中间变量?
发布于 2014-05-17 12:35:23
在取消引用运算符上使用decltype()时,删除引用:
using type = typename std::remove_reference<decltype(*(A.shape()))>::type;这是因为取消引用操作符返回对迭代器所指向的基础元素的lvalue引用(以便能够对其进行读写)。
https://stackoverflow.com/questions/23711355
复制相似问题