Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 580817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:30:48+00:00 2026-05-13T14:30:48+00:00

I have a problem with this bit of code: #include <boost/multi_array.hpp> #include <boost/array.hpp> #include

  • 0

I have a problem with this bit of code:

#include <boost/multi_array.hpp>
#include <boost/array.hpp>
#include <vector>
#include <iostream>

template <typename Vec>
void foo(Vec& x, size_t N)
{
    for (size_t i = 0; i < N; ++i) {
        x[i] = i;
    }
}

int main()
{
    std::vector<double> v1(10);
    foo(v1, 5);
    std::cout << v1[4] << std::endl;


    boost::multi_array<double, 2> m1;
    boost::array<double, 2> shape;
    shape[0] = 10;
    shape[1] = 10;
    m1.resize(shape);
    foo(m1[0], 5);
    std::cout << m1[0][4] << std::endl;
    return 0;
}

Trying to compile it with gcc, I get the error:

boost_multi_array.cpp: In function 'int main()':
boost_multi_array.cpp:26: error: invalid initialization of non-const reference of type 'boost::detail::multi_array::sub_array<double, 1u>&' from a temporary of type 'boost::detail::multi_array::sub_array<double, 1u>'
boost_multi_array.cpp:7: error: in passing argument 1 of 'void foo(Vec&, size_t) [with Vec = boost::detail::multi_array::sub_array<double, 1u>]'

It works as expected for boost::multi_array when I change the type of the first argument of function foo from Vec& to Vec, but then the std::vector is passed by value, which is not what I want. How can I achieve my goal without writing two templates?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:30:48+00:00Added an answer on May 13, 2026 at 2:30 pm

    The problem is that for NumDims > 1, operator[] returns a temporary object of type template subarray<NumDims-1>::type.

    A (not so nice) work-around would be the something like the following:

    typedef boost::multi_array<double, 2> MA;
    MA m1;
    MA::reference ref = m1[0];
    foo(ref, 5); // ref is no temporary now
    

    An alternative would be to wrap your implementation and provide an overload for the multi-array case…. E.g.:

    (note: i didn’t see how to get the overload to work with boost::multi_array<T,N>::reference, please don’t put it into productive use with this detail:: version 😉

    template<class T>
    void foo_impl(T x, size_t N) {
        for (size_t i = 0; i < N; ++i) {
            x[i] = i;
        }
    }
    
    template<class T>
    void foo(T& t, size_t n) {
        foo_impl<T&>(t, n);
    }
    
    template<typename T, size_t size>
    void foo(boost::detail::multi_array::sub_array<T, size> r, size_t n) {
        foo_impl(r, n);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 519k
  • Answers 519k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer display:inline; Paddings are not applicable to inline elements. You need… May 16, 2026 at 8:25 pm
  • Editorial Team
    Editorial Team added an answer Fluent API for building Commands in WPF http://code.google.com/p/present/ May 16, 2026 at 8:25 pm
  • Editorial Team
    Editorial Team added an answer One solution: Save the image somewhere in a temorary folder… May 16, 2026 at 8:25 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I've got a bit of a problem. Essentially, I need to store a large
i have a problem with the order of execution of the threads created consecutively.
this is a follow up to my recent question ( Code for identifying programming
I have a wierd problem that i need to work out how to resolve,
I have a small problem. After making a hex mask, I can not copy/paste
I have a text file which was created using some Microsoft reporting tool. The
We have a C++ library that we provide to several different clients. Recently we
My comments on this answer got me thinking about the issues of constness and
I'm writing some JNI code in C++ to be called from an applet on
I'm currently learning a bit more about Linq-To-Entities - particularly at the moment about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.