1

The following piece of code does not compile:

#include <valarray>

int main()
{
   std::valarray<std::valarray<int>> a;
   std::valarray<std::valarray<int>> b;
   //std::valarray<std::valarray<bool>> c;
   std::valarray<bool> c;
   c = (a == b);
   return 0;
}

Neither of the declarations of c compile. Is it allowed to use valarray in the above demonstrated manner or are there restrictions on how it should be used.

Rikardo Koder
  • 672
  • 9
  • 16

1 Answers1

1

I believe I have the solution, though I may be wrong.

Because begin and end are non-member overloads, you use begin(a) instead of a.begin() and so on.

My choice in using std::transform is that you want to iterate over a and b, compare them, and store the results in c, which is why the lambda returns a std::valarray<bool> (instead of a bool).

   std::valarray<std::valarray<int>> a;
   std::valarray<std::valarray<int>> b;
   std::valarray<std::valarray<bool>> c;

   std::transform(begin(a), end(a), begin(b), begin(c),
   [&] (std::valarray<int> a_val, std::valarray<int> b_val) 
       -> std::valarray<bool> {
       return std::valarray<bool>(a_val == b_val);
   });
  • That is an interesting work around, however the inequality operators having been defined for valarray imply that perhaps such a work around "should" not be required. – Rikardo Koder Nov 29 '13 at 05:15
  • @Rikardo I'm not sure if it's possible though. You're trying to use a single assignment operator for nested valarrays. Of course further research is required, but the workaround was easier to come up with than a reason for why your code would not work (other than the compile errors.) –  Nov 29 '13 at 05:20