1

I have a wrapper data structure that I'd like to write a method for the copy assignment operator. I'd really like to do this (because my byte_array can do some really cool stuff):

byte_array bytes = {0x00, 0xAB, 0xEE, 0xFF};

and the function I'm trying to write looks like this so far:

template <int N>
byte_array& byte_array::operator = (const unsigned char (&a)[N]) {
    if(N <= capacity) { //capacity is a class field
        for(int i = 0; i < N; i++) array[i] = a[i]; //array is a class field
        usage = N; //usage is a class field
    }
    return *this;
}

However, this code doesn't compile, as it does not like that I gave it a list. It doesn't like a lot of things about this.

Is there something wrong with the function I wrote, such as the assignment operator only working on objects of the same type (like a conflict of byte_array and unsigned char[])? Is it an issue of my 'list' that I'm trying to assign it to not matching up with the unsigned char[] type?

I was basing my function off of an answer to this past question about arrays in C++.

  • maybe use the type const std::initializer_list& instead of an array type as {0x00, 0xAB, 0xEE, 0xFF} is not an array – Nyashes Jun 12 '17 at 14:39

2 Answers2

3

The issue with

byte_array bytes = {0x00, 0xAB, 0xEE, 0xFF};

is that you you do not actually have an array on the right hand side. You have an initializer list, which doesn't have a type.

What you can do though is take a std::initializer_list<unsigned char> and that can be constructed from the initializer list.

In C++98 you can create a temporary array and then use that array from the assignemnt like

byte_array bytes;
//some code
{
    unsigned char temp[] = {0x00, 0xAB, 0xEE, 0xFF};
    bytes = temp;
}
// more code

Also note that Type name = initializer; is never assignment. It is called copy initialization/copy list initialization(if initializer is a initializer list), and calls the copy/move constructor of the object. The assignment operator is only ever called then you have an already constructed object.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • http://www.cplusplus.com/reference/initializer_list/initializer_list/ says that `std::initializer_list` is something that was introduced in c++11; however, I must deal with c++98... –  Jun 12 '17 at 14:45
  • 1
    @Peri461 OK. I added the C++98 tag to your question so that we know as the C++ tag currently means C++14. AFAIK, this is not possible in C++98. You would have to create an array and then use that array for the assignment. – NathanOliver Jun 12 '17 at 14:47
  • Yikes. Glad to know it's not possible, at least. Thanks. I apologize for not specifying the C++ version ahead of time. –  Jun 12 '17 at 14:50
  • @Peri461 If you want to do *really cool stuff*, then, I'm afraid, you better use C++11. Otherwise, it's too painful to do cool stuff. – Walter Jun 12 '17 at 15:56
0

the issue is that you are trying to assign an initializer_list {a, b, c} and not an array. You might want to take an initializer list as the parameter instead:

#include <iostream>


struct byte_array
{
    byte_array& operator = (const std::initializer_list<unsigned char>& tab) {
        for (unsigned char item: tab)
            std::cout << item << "\n";
        return *this;
    }
};

int main()
{
  byte_array bytes;
  bytes = {'a', 'b', 'c', 'd'};
  (void)bytes;
}

run it here: http://cpp.sh/2vv6m

Nyashes
  • 652
  • 4
  • 22