14

I read about std::valarray in a C++ book writen by Nicolai M. Josuttis. He writes in his book The C++ Standard Library, chapter 17.4:

The valarray classes were not designed very well. In fact, nobody tried to determine whether the final specification worked. This happened because nobody felt “responsible” for these classes. The people who introduced valarrays to the C++ standard library left the committee long before the standard was finished. As a consequence, valarrays are rarely used.

So, where is it a good idea to use std::valarray?

Jayesh
  • 4,755
  • 9
  • 32
  • 62
  • 1
    I'd say "yes", but that's an opinion, just like what Nicolai wrote (except his perhaps carries more weight). I am sure you could find a legitimate use case for them *somewhere*, but in most cases there simply exist better alternatives. – Bartek Banachewicz Sep 28 '17 at 08:58
  • 2
    ^ --- I concur, but it doesn't make this post less opinion-based. Maybe you should rephrase? – StoryTeller - Unslander Monica Sep 28 '17 at 08:58
  • I think the question could be somewhat reasonably answered if it asked for places where it's a good idea to use it. I can't readily find any, though. – Bartek Banachewicz Sep 28 '17 at 09:01
  • 1
    Imho it makes sense, when you need to do some simple vector math, don't care about performance and don't already have a dependency on a library that does a better job. Imho that combination is rather rare. – MikeMB Sep 28 '17 at 10:06
  • 2
    dupe? https://stackoverflow.com/questions/1602451/c-valarray-vs-vector – default Sep 28 '17 at 10:07
  • I really really like to know some real-world applications of valarray. Please shed some light. – Saeed Amrollahi Boyouki Sep 28 '17 at 13:22
  • This seems too broad/opinion-based, but anyway, very likely a duplicate of [C++ valarray vs. vector](https://stackoverflow.com/questions/1602451/c-valarray-vs-vector) Also, simply looking at the interface/documentation of the class ought to indicate what it's good for, no? – underscore_d Sep 29 '17 at 07:07
  • "rarely used" is relative. https://codesearch.debian.net/search?q=valarray gives 42k results – Cubbi Sep 29 '17 at 17:10

1 Answers1

3

Valarrays are created to allow the compiler to make faster executable. However, these optimizations are achievable by other means and valarrays are rarely used.

std::valarray and helper classes are defined to be free of certain forms of aliasing, thus allowing operations on these classes to be optimized similar to the effect of the keyword restrict in the C programming language. In addition, functions and operators that take valarray arguments are allowed to return proxy objects to make it possible for the compiler to optimize an expression such as v1 = av2 + v3; as a single loop that executes v1[i] = av2[i] + v3[i]; avoiding any temporaries or multiple passes. .....

In other words, valarrays are meant to be used in places when there are a lot of values which are passed mostly to simple expressions.

Only rarely are valarrays optimized any further, as in e.g. Intel Parallel Studio.

You can find more information at cppreference

Petar Velev
  • 2,305
  • 12
  • 24