-1

Hello I have a class like std::vector and I want to be able to do this: MyVec<int> vec(2, 5, 91, 27);
this is the code:

    template <typename... args>
    explicit MyVec(const args&... _args)
    {
        // some code...

        size_t i = 0;
        (void(m_data[i++] = _args), ...); // ERROR C2679
    }

I don't understand how fold expressions work this is my first time using it so I wonder what am I doing wrong here and how to use fold expressions correctly?

Error: Severity Code Description Project File Line Suppression State Error C2679 binary '=': no operator found which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)

So for some reason I think _args is type of const int or something when I cast it to type T which in above example would be int like this void(m_data[i++] = *(T*)&_args), ...); error is gone but m_data contains integers of valye -842150451. what is going on there?

  • What exactly is the error message? What type is `m_data`? I would sugguest you consider a constructor with `std::initializer_list` instead. – Weijun Zhou Sep 02 '23 at 08:59
  • yes but i'm trying to not use std::initializer_list – Tornike Kacadze Sep 02 '23 at 09:09
  • Your code will just work with the proper type of `m_data`. So the fold expression itself is fine. You really need to show what type `m_data` is. Demo: https://godbolt.org/z/vEed59Eqh. Note I'm not saying that using `std::any` is a good idea or can actually solve your underlying issue. – Weijun Zhou Sep 02 '23 at 09:11
  • MyVec is template class and m_data is an array of type T. T* m_data, which is allocated in the constructor. – Tornike Kacadze Sep 02 '23 at 09:17
  • 1
    Please show the complete definition of the class `MyVec` as well as the constructor, otherwise it's all guesswork. – Weijun Zhou Sep 02 '23 at 09:22

0 Answers0