据说如果我们想更有效地使用multi_array,我们最好使用multi_array builder。但是,我对模板和 boost 都很陌生,我试图从书中复制一些代码。它看起来像这样:
class multi_builder : boost::noncopyable
{
public:
typedef boost::multi_array<T,N> array_type;
typedef boost::shared_ptr<array_type > type;
private:
boost::any ext;
public:
multi_builder() : ext(boost::extents){}
~multi_builder(){}
template<std::size_t n>
void dim(std::size_t x)
{
BOOST_STATIC_ASSERT(n >= 0 && n < N);
ext = boost::any_cast<boost::detail::multi_array::extent_gen<n> >(ext) [x];
}
boost::type create(void)
{
return boost::type<array_type>(new array_type(boost::any_cast<boost::detail::multi_array::extent_gen<N> >(ext)));
}
};
但是,当我尝试在这样的代码中使用它时:
multi_builder<int,2> builder;
builder.dim<0>(2);
builder.dim<1>(2);
BOOST_AUTO(mp,builder.create());
for(int i = 0,v = 0; i < 2; ++i)
for(int j = 0; j < 2; ++j)
(*mp)[i][j] = v++;
编译器生成以下错误:
error:invalid use of template-name 'boost::type' without an argument list
error:'class multi_builder<int, 2u>' has no member named 'create'.
error:invalid type in declaration before '=' token
error:'class multi_builder<int, 2u>' has no member named 'create'
error:invalid type argument of 'unary *'
有人可以告诉我如何解决这些错误吗?