Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
[multiarray] problems passing views by reference
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Rhys Ulerich  
View profile  
 More options Jan 5 2010, 11:23 pm
From: Rhys Ulerich <rhys.uler...@gmail.com>
Date: Tue, 5 Jan 2010 22:23:30 -0600
Local: Tues, Jan 5 2010 11:23 pm
Subject: [Boost-users] [multiarray] problems passing views by reference

Recently, Hanan Sadar asked about a multi_array fill implementation
[1].  The standard std::fill_n(x.data(),x.num_elements(),v) answer
only works when x is a boost::multi_array or boost::multi_array_ref
but does not handle the general MultiArray concept.

I decided to implement an arbitrary dimensional MultiArray fill
capable of handling such views correctly.  It's been absolutely
miserable for reasons I don't understand but which resemble comments
from James Amundson's recent thread [2].  Attached is my attempt with
two exasperating lines marked by "Cannot use reference, why?".

For a non-multi_array, non-multi_array_ref instance (i.e. a view)
using a signature like
    template< class Array, class V > void operator()(Array x, const V &v);
allows the code to compile and operate correctly.  However, it incurs
pass-by-value overhead.  Using
a signature like
    template< class Array, class V > void operator()(Array &x, const V &v);
using pass-by-reference dies at compilation time.  All of my attempts
to use MultiArray's associated typedefs die, including hours of
mucking with MultiArray::reference.

What am I missing here?  Why can't I pass a general MultiArray view
type by reference?

- Rhys

[1] http://groups.google.com/group/boost-list/browse_thread/thread/829ebe...
[2] http://groups.google.com/group/boost-list/browse_thread/thread/4531a7...

  test_fill.cc
5K Download

_______________________________________________
Boost-users mailing list
Boost-us...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ronald Garcia  
View profile  
 More options Jan 11 2010, 4:11 pm
From: Ronald Garcia <r...@cs.cmu.edu>
Date: Mon, 11 Jan 2010 16:11:30 -0500
Local: Mon, Jan 11 2010 4:11 pm
Subject: Re: [Boost-users] [multiarray] problems passing views by reference
Hi Rhys,

The problem might be the body of fill_functor for the general case.  
"*i" will produce a temporary (a subarray), which you are then trying  
to pass immediately by reference to the smaller fill_functor case.  C+
+ doesn't let you pass temporaries by reference, which is a real  
problem when you need proxy references like MultiArray does.  The  
solution should be as follows:

         for (typename Array::iterator i = x.begin(); i != x.end(); +
+i) {
           typename Array::iterator::reference ri = *i;
             f(ri,v);
         }

That is, assign the temporary to a variable before you call f.

Cheers,
Ron

On Jan 5, 2010, at 11:23 PM, Rhys Ulerich wrote:

_______________________________________________
Boost-users mailing list
Boost-us...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rhys Ulerich  
View profile  
 More options Jan 13 2010, 11:48 am
From: Rhys Ulerich <rhys.uler...@gmail.com>
Date: Wed, 13 Jan 2010 10:48:23 -0600
Local: Wed, Jan 13 2010 11:48 am
Subject: Re: [Boost-users] [multiarray] problems passing views by reference

> The problem might be the body of fill_functor for the general case.  "*i"
> will produce a temporary (a subarray), which you are then trying to pass
> immediately by reference to the smaller fill_functor case.  C++ doesn't let
> you pass temporaries by reference, which is a real problem when you need
> proxy references like MultiArray does.  The solution should be as follows:

>        for (typename Array::iterator i = x.begin(); i != x.end(); ++i) {
>          typename Array::iterator::reference ri = *i;
>            f(ri,v);
>        }

Ron's suggestion did fix my problem.  Thanks Ron.

Just in case anyone's ever searching for a general MultiArray fill
implementation, I've attached the now working code/test case.

This might not be a bad example to add to the MultiArray tutorial, both
because it touches on an easy mistake and because people ask about
fill with some frequency.

- Rhys

  test_fill.cc
5K Download

_______________________________________________
Boost-users mailing list
Boost-us...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ronald Garcia  
View profile  
 More options Jan 14 2010, 4:06 pm
From: Ronald Garcia <r...@cs.cmu.edu>
Date: Thu, 14 Jan 2010 16:06:07 -0500
Local: Thurs, Jan 14 2010 4:06 pm
Subject: Re: [Boost-users] [multiarray] problems passing views by reference
Thanks for the contribution Rhys.  I think it will make a fine  
addition to MultiArray.

Best,
Ron

On Jan 13, 2010, at 11:48 AM, Rhys Ulerich wrote:

_______________________________________________
Boost-users mailing list
Boost-us...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »