473,833 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

valarray::opera tor[]

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_iterato r<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<typena me T>
T valarray<T>::op erator[](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 1796
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_iterato r<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<typena me T>
T valarray<T>::op erator[](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
3670
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. Multiplying a double by a complex number gives a complex number. Now I would like to multiply a valarray of doubles by a complex number and get a valarray of complex numbers. Of course, a C++ compiler won't jump to that conclusion; I need to define...
2
5407
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 class "MoreComplex" which is derived from std::complex. It looks real good, and compiles until I try to call an operator. Then, I get a link error since it can't find the operator. I think that it's too abstract, with all these templates the...
0
1721
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 involve needless copies. Example code follows: #include <iostream> #include <valarray>
1
1381
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 including Comeau produced an error message
2
2061
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 ) : std::valarray<T>( sz ) {}
10
6319
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
2626
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
1952
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 could not find anything that explains the need of this "operator+".
43
4855
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
9796
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10782
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10500
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10543
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9323
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6951
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5789
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3972
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3078
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.