473,473 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

valarray::operator[]

comp.std.c++ would be a better place for this question.
Forgive me, but I can't post anything on moderated newsgroups
for some reason.

valarray doesn't have iterators of its own, which makes functions
in <algorithm> less useful. &v[0] works like an iterator, but even this
trick doesn't work for const valarray's.

void f(const valarray<double>& v)
{
copy(&v[0], &v[v.size()], ostream_iterator<int>(cout, " "));
}

This code doesn't compile (and it looks ugly, I'd say) since you can't
take the address of an r-value:

template<typename T>
T valarray<T>::operator[](size_t) const;

I'm curious the reason why the return type is T instead of const T& ?
I know valarray's are not meant for an ordinary container, but I think
there would be nothing to lose for taking advantage of const-correctness.

--
ES Kim
Jul 22 '05 #1
1 1765
ES Kim wrote:
comp.std.c++ would be a better place for this question.
Forgive me, but I can't post anything on moderated newsgroups
for some reason.

valarray doesn't have iterators of its own, which makes functions
in <algorithm> less useful. &v[0] works like an iterator,
but even this trick doesn't work for const valarray's.

void f(const valarray<double>& v) {
copy(&v[0], &v[v.size()], ostream_iterator<int>(cout, " "));
}

This code doesn't compile (and it looks ugly, I'd say)
since you can't take the address of an r-value:

template<typename T>
T valarray<T>::operator[](size_t) const;

I'm curious the reason why the return type is T instead of const T&?
I know valarray's are not meant for an ordinary container,
but I think there would be nothing to lose
for taking advantage of const-correctness.


valarray is a numerical class library
which probably doesn't really belong in the standard library.
I believe that Kent Budge was thinking that,
if valarray was part of the standard library,
C++ compiler developers might be encouraged
to treat a valarray as a built-in type
and aggressively optimize on them
for fast vector processing super computers.
Unfortunately, C++ compiler developers
couldn't be convinced to invest in optimized compilers
for people that were going to use Fortran 90 anyway.

Iteration is evil as far as numerical computing is concerned.
It virtually precludes any kind of parallelization.

valarray really wasn't meant to be generic.
T really must be a real or complex floating-point number.
valarray is also paranoid about aliases.
Return by value avoids creating an alias
for part of a valarray.

valarrays probably should never appear
in the body of your program.
They are really best used as private members
of a vector and/or matrix class library.

Jul 22 '05 #2

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

Similar topics

6
by: Christian Brechbühler | last post by:
The template std::valarray behaves pretty much like a mathematical vector. Arithmetic operators apply elementwise. Now I'd like to extend this to a user-defined type, e.g., complex. ...
2
by: Jerry Krinock | last post by:
Hi, I'd like to make a class of complex numbers (real, imaginary) which has an extra number (real, imaginary, extraFoo) and be able to use operators on arrays of them. So I declared a template...
0
by: Prune Tracy | last post by:
I have a question about assignment of slices of valarrays. I want to assign one general slice of a valarray to another. It seems the only way is to use the static_cast syntax, but this seems to...
1
by: ES Kim | last post by:
Here's a code fragment from TC++PL, p679: void f(valarray<double>& v) { size_t i = { 3, 2, 1, 0 }; valarray<size_t> index(i, 4); valarray<double> vv = log(v); } Every compiler I tried...
2
by: Michael Hopkins | last post by:
Hi all I have a subclass of valarray<T> thus template <typename T> class uo_val : public std::valarray<T> { public: uo_val ( ) : std::valarray<T>() {} uo_val (const int sz ) :...
10
by: Chris Forone | last post by:
Hello Group, there is some memberfunc for std::valarray to return a pointer to the first element in the array. How do i use this? Thanx a lot. HAND Chris
5
by: Chris Forone | last post by:
Hello group, g++ (3.4.2, mingw): float init = {1.f, 2.f, 3.f}; std::map<std::string, std::valarray<float mp; mp = std::valarray(init, 3); mp.size(); // should be 3, but IS 0!
2
by: john | last post by:
Hi, in TC++PL3 on page 665, regarding valarray member functions, it is mentioned: "valarray operator-() const; // result= -v for every element // similarly: +, ~, !" I checked the web and...
43
by: john | last post by:
Hi, in TC++PL 3 on pages 674-675 it is mentioned: "Maybe your first idea for a two-dimensional vector was something like this: class Matrix { valarray< valarray<doublev; public: // ... };
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
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
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.