Another quirk?

Ulrich Drepper
Sun Nov 25 14:02:00 GMT 2012

Compile the following code, once with -DCONST=const and once without
the define.  The first will succeed, the second doesn't.

I know why this happens.  With the const valarray references to the
original array can be kept and no copies have to be made.  In fact,
when I introduce an intermediate variable of type valarray<int> and
use the result of the slice operation to initialize it and then use
sum() with the new variable everything works fine, too.

The question is: is this really the intended behavior?  I seems like
the conversion from std::slice_array<Tp> to std::valarray<Tp> should
happen implicitly and thus allowing the original code to work.

Comments?  I'll file a bug in case my expectation is correct.

#include <valarray>
#include <iostream>

#ifndef CONST
#define CONST

int main() {
  static const int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  constexpr size_t narr = sizeof(arr) / sizeof(arr[0]);
  CONST std::valarray<int> va(arr, narr);
  std::cout << va[std::slice(0, 3, 4)].sum() << std::endl;

More information about the Libstdc++ mailing list