0

It is said that we'd better use a multi_array builder if we want to use the multi_array more efficiently. However, I'm so new to both template and boost, I tried to copy some code from a book. It looks like this:

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

However, when I tried to use it in the code like this:

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++;  

the compiler generates the following errors:

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 *'

Could someone tell me how to fix the errors?

user957121
  • 2,946
  • 4
  • 24
  • 36

2 Answers2

0

From the looks of it, the return type of create() lacks a template argument list. I haven't used this Boost component but based on how the value is returned it should probably look like this:

boost::type<array_type> create(void)
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Thanks for the reply. However, it doesn't work. I still get 2 errors like 'no match for 'operator*' in '*mp'' and 'no matching function for call to 'boost::type > >::type(boost::multi_array >*)''candidates are: boost::type > >::type() – user957121 Sep 30 '12 at 03:10
0

There are several problem with the code.

First you are probably missing template parameters for the class (as the other answer pointed out).

Second, create seems to return a smart pointer (shared_ptr) that you define as type in your class (bad name by the away).

This code now compiles and runs: https://godbolt.org/z/qs19eYfso

#include<boost/any.hpp>
#include<boost/multi_array.hpp>
#include<boost/shared_ptr.hpp>

template<class T, std::size_t N>
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];
    }
    type create(void)
    {
        return type(new array_type(boost::any_cast<boost::detail::multi_array::extent_gen<N> >(ext)));
    }
};

int main() {
    multi_builder<int,2> builder;
    builder.dim<0>(2);  
    builder.dim<1>(2);  
    auto mp = builder.create();
    for(int i = 0,v = 0; i < 2; ++i)
        for(int j = 0; j < 2; ++j)
            (*mp)[i][j] = v++;  
}
alfC
  • 14,261
  • 4
  • 67
  • 118