0

Can we be sure that valarray is a specialized class template ? and that's why it doesn't let implicite conversion happen ?

  std::valarray<int> values={1.0,2.0};

    for(int it: values ){

        std::cout<<it<<std::endl;
    }

error: narrowing conversion of '1.1000000000000001e+0' from 'double' to 'int' [-Wnarrowing]

rekkalmd
  • 171
  • 1
  • 12
  • 5
    You are constructing `std::valarray` using `std::initializer_list` if that is your question – Cory Kramer May 15 '20 at 16:18
  • 3
    A `double` literal (like `1.0`) is not an `int`, and conversion from a `double` (literal or not) to an `int` involves narrowing. That's what the `-Wnarrowing` warning will warn about. – Eljay May 15 '20 at 16:21
  • I suspect what you wanted was `std::valarray values={1, 2};` - that is, initialization using `int`egers, not `double`s. – Jesper Juhl May 15 '20 at 16:26

0 Answers0