473,441 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

Retaining a handle on a std::indirect_array<>

I believe the answer is that the language doesn't support the ability to
retain a reference to a std::indirect_array<>, and would be illadvised to
try, but I figure I'll ask just to be sure. What I mean by retaining a
handle is that I would like to have a class that has a member field
functioning as a reference into a valarray initialized by an
indirect_array.

template<typename T>
class IA{
indirect_array<T>& ia_;
public:
IA(indirect_array<T>& ia_):_ia(ia_){}
}
This code fails with the arcana listed following:

#include <sth/sth_std.hh> // every blinkin' standard header there is

using std::valarray;
using std::indirect_array;
using std::cout;
using std::ostream;

template<typename T>
ostream& operator<<(ostream& out, const valarray<T>& va) {
for(size_t i; i < va.size(); i++) {
out << va[i] << " ";
}
return out;
}

template<typename T>
class Indirect {
public:
Indirect(indirect_array<T>& v_ )
:_v(v_)
{}

ostream& print(ostream& out) const {
return out << "Indirect: " << valarray<T>(_v) << "\n";
}

void operator *= (const T& t)
{
_v *= t;
}

protected:
indirect_array<T>& _v;
valarray<size_t>& idx;
};

template<typename T>
ostream& operator<<(ostream& out, const Indirect<T>& i) { return
i.print(out); }

void test() {
double da[9] =
{1.1 , 1.2, 1.3
,2.1, 2.2, 2.3
,3.1, 3.2, 3.3};

valarray<double> v(da,sizeof(da)/sizeof(da[0]));
size_t ia[3] = {0,3,6};
valarray<size_t> idx(ia, sizeof(ia)/sizeof(ia[0]));

Indirect<double> id(v[idx]);
cout << id;
}

int main(){
test();
}

-*- mode: compilation; default-directory: "~/code/c++/scratch/valarray/" -*-
g++ -oindirect indirect.cc -I$CPPSTH -I$BOOST_INCLUDE
indirect.cc: In function `void test()':
indirect.cc:50: error: no matching function for call to `Indirect<double>::
Indirect(std::indirect_array<double>)'
indirect.cc:17: error: candidates are: Indirect<double>::Indirect(const
Indirect<double>&)
indirect.cc:20: error:
Indirect<T>::Indirect(std::indirect_array<_Tp>&) [with T = double]
distcc[2926] ERROR: compile on localhost failed
I believe the penultimate line is telling me to pound sand, but I'm not
sure. Note that I wrote /indirect_array<T>&/ and
not /indirect_array<_Tp>&/.

The alternative I am considering is to maintain an index (actually present
in class Indirect{}) and use that to form the indirect_array on the fly as
needed. That adds significantly to the memory usage, but I can probably
live with it. Is there a better alternative? (I'm aware of slice and
gslice).
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #1
2 1525
On Wed, 10 Nov 2004 03:31:26 -0500, "Steven T. Hatton"
<su******@setidava.kushan.aa> wrote:
I believe the answer is that the language doesn't support the ability to
retain a reference to a std::indirect_array<>, and would be illadvised to
try, but I figure I'll ask just to be sure.
The standard has a specific (non-normative) note saying that users
should never declare a std::indirect_array - it effect, you should
only use it as an rvalue.

What I mean by retaining ahandle is that I would like to have a class that has a member field
functioning as a reference into a valarray initialized by an
indirect_array. [snip]-*- mode: compilation; default-directory: "~/code/c++/scratch/valarray/" -*-
g++ -oindirect indirect.cc -I$CPPSTH -I$BOOST_INCLUDE
indirect.cc: In function `void test()':
indirect.cc:50: error: no matching function for call to `Indirect<double>::
Indirect(std::indirect_array<double>)'
indirect.cc:17: error: candidates are: Indirect<double>::Indirect(const
Indirect<double>&)
indirect.cc:20: error:
Indirect<T>::Indirect(std::indirect_array<_Tp>&) [with T = double]
distcc[2926] ERROR: compile on localhost failed
I believe the penultimate line is telling me to pound sand, but I'm not
sure. Note that I wrote /indirect_array<T>&/ and
not /indirect_array<_Tp>&/.
You can't bind a temporary to a non-const reference, as you are
attempting in the line:
Indirect<double> id(v[idx]); //v[idx] returns a temporary
The alternative I am considering is to maintain an index (actually present
in class Indirect{}) and use that to form the indirect_array on the fly as
needed. That adds significantly to the memory usage, but I can probably
live with it. Is there a better alternative? (I'm aware of slice and
gslice).


Yes, instead of holding the indirect_array, you should be holding the
valarray<size_t>, and creating the indirect_array on demand. However,
really you should be using a library that is likely to be more
efficient, like uBLAS. valarray is very hard to use efficiently, since
potentially arrays are copied unnecessarily all the time.

Tom
Jul 22 '05 #2
With reference to your class declaration Steven:
template<typename T>
class IA{
indirect_array<T>& ia_;
public:
IA(indirect_array<T>& ia_):_ia(ia_){}
}

Your constructor is initializing the member refernce _ia with ia_,
however _ia doesn't exist, and you also lack a semicolon after the
closing brace. Here's a revised piece of code to clarify what I mean:
template<typename T>
class IA{
indirect_array<T>& ia_;
public:
IA(indirect_array<T>& _ia):ia_(_ia){}
}; //i changed the parameter name from ia_ to _ia so that there will
be no name
//conflicts with the member reference

This may not have helped you solve your problem but I hope that it is
helpful to you. :)

Benjamin Lau
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
by: SamMan | last post by:
A question came up at work from one of our clients about forms on their site. The data from these forms are processed by a PHP script and if all goes well, a thank you screen appears. Sometimes,...
8
by: Deepa | last post by:
I am writing a console app in c# wherein am converting a dataset into a CSV file. It works fine. But I have some values in the dataset which have a comma within(eg. A,B,C). When I view the CSV file...
1
by: antonyliu2002 | last post by:
This was originally posted as a reply to a thread, but it soon sunk to the bottom, and nobody was paying attention to this. My problem is how to effectively retain user-supplied form data. ...
2
by: John Salerno | last post by:
If I read a string that contains a newline character(s) into a variable, then write that variable to a file, how can I retain those newline characters so that the string remains on one line rather...
4
by: Jim Carlock | last post by:
$_SERVER returns the root path for the subdomain, which is different than the root path for the primary domain. Is there a global variable to get the path for the root domain? What's the best...
5
by: David Cartwright | last post by:
Hi all, I'm using a DataGrid to present a tabulated list (actually a list of users logged in to my phone system) - it seemed the most appropriate control. As stuff happens on the phone system,...
3
by: Rob Dob | last post by:
Hi, Does anyone know of a good, cheap, prefereably free ,net control that I can use that will aloow users to not only rate something i.e.: 1-5 stars but also post comments on whatever they are...
3
by: xscape | last post by:
Hi all, I have an XML file which contains a load of nested divs. I'm trying to write some XSL to create a new XML file with all the excess divs removed, but I want to retain the content and place...
2
by: s4lin | last post by:
problem is pagination not retaining data field i have form with general data field, and i need to select some option which is store in database, so i open another page with retrieving data from...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.