S
Steven T. Hatton
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:
stream;
template<typename T>
ostream& operator<<(ostream& out, const valarray<T>& va) {
for(size_t i; i < va.size(); i++) {
out << va << " ";
}
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
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:
template<typename T>
ostream& operator<<(ostream& out, const valarray<T>& va) {
for(size_t i; i < va.size(); i++) {
out << va << " ";
}
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