2
struct Data{
    Data() = default;
    Data(std::initializer_list<float> list) {
>>   std::copy(list.begin()    , list.begin() + 4, std::begin(data));
    }
    float data[4] =  { 0, 0, 0, 1 };
  };

warning C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'

Since I need this data to be into a standard-layout structure which I can stitch into a memory buffer, I can't use std::array.

How can I solve this problem and/or workaround it without disabling the warning?

Dean
  • 6,610
  • 6
  • 40
  • 90
  • 1
    Why exactly can't you use `std::array`? – Ely Dec 22 '15 at 08:03
  • Why does std::array interfere with standard layout? – Nir Friedman Dec 22 '15 at 08:04
  • 1
    Because http://stackoverflow.com/questions/34393066/is-the-size-of-stdarrayt-n-guaranteed-to-be-equal-to-the-size-of-tn , I need this struct to be equal to 4 floats – Dean Dec 22 '15 at 08:06
  • Just because something is a warning, doesn't mean it should be heeded. The warning is on by default for beginners, because beginners wouldn't know how to turn it on if it was off. Turn it off. – Benjamin Lindley Dec 22 '15 at 08:12
  • Have you tried unrolling the loop? Also, shouldn't you `assert()` that the initializer list contains at least (or exactly?) four elements? That said, I'm not sure if you use "checked iterators" at all there, which parameter exactly does the warning complain about? – Ulrich Eckhardt Dec 22 '15 at 08:30
  • @UlrichEckhardt: The warning is about `std::begin(data)`, because it's a pointer, which can't be checked. – Benjamin Lindley Dec 22 '15 at 08:36
  • I added some bound-checking as Ulrich suggested, anyway as Benjamin noted this problem is unrelated and still persists. – Dean Dec 22 '15 at 08:43
  • Why not just go to list.end() and check that the size of the initializer list is four? I agree with the warning, as written it is dangerous and easy to misuse. – Nir Friedman Dec 22 '15 at 08:47
  • @NirFriedman I just added that code but it will not solve the warning. The world is wider than a playground and I can't always check everything (as in this case). – Dean Dec 22 '15 at 08:50
  • I don't understand, you wrote std::copy(list.begin(), list.end(), begin(data)) and you still get the warning? – Nir Friedman Dec 22 '15 at 08:54
  • Does using `std::array` instead of `std::initializer_list` remove the warning ? – Jarod42 Dec 22 '15 at 09:35
  • 1
    As workaround, you may do a manual loop of assignment... – Jarod42 Dec 22 '15 at 10:15

0 Answers0