Why can an initializer list only be used on declaration?
Is there a reason for having such a feature at declaration time but not once the array is declared? Why does it work in one case but not in the other case?
The x = {a, b, ...}
syntax involves a specific type of initializer lists called copy-list-initialization. The cppreference mentions the possible ways to use copy-list initialization:
T object = {arg1, arg2, ...};
(6)function( { arg1, arg2, ... } )
(7)return { arg1, arg2, ... } ;
(8)object[ { arg1, arg2, ... } ]
(9)object = { arg1, arg2, ... }
(10)U( { arg1, arg2, ... } )
(11)Class { T member = { arg1, arg2, ... }; };
(12)
The array syntax that you tried T myArr[] = {a, b, c...}
works and is numerated as (6) initialization of a named variable with a braced-init-list after an equals sign.
The syntax that doesn't work for you (myArr = {a, b, ...}
) is numerated as (10) and it's called list-initialization in an assignment expression. The thing about assignment expressions is that the left side must be a so-called lvalue, and although arrays are lvalues, they cannot appear on the left side of assignments as per the specification.
That being said, it wouldn't be very hard to work around assignment by copying an initializer list onto the array as such:
#include <algorithm>
#include <iostream>
int main() {
int arr[] = {1, 2, 3};
auto itr = arr;
auto assign = {5, 2, 1};
std::copy(assign.begin(), assign.end(), itr);
}
Arrays are second-class citizens in C++.
They are objects,
but they are severely restricted:
they can't be copied,
they are decayed into pointers in various contexts, etc.
Consider using std::array
,
which is a (fixed-size) wrapper on top of builtin arrays,
but is a first-class citizen which supports various convenience features:
std::array<int, 3> my_array = {10, 20, 30};
my_array = {40, 50, 60};
This works because, per [array.overview]/2,
std::array
is an aggregate type that can be list-initialized with up toN
elements whose types are convertible toT
.
live demo
This also works with std::vector
.
Vectors are a different story,
so I am not going to go into details here.
If you prefer to insist on builtin arrays, here's a workaround I designed to enable assigning a list of values to a builtin array (respecting value categories), using template metaprogramming techniques. A compile-time error is (correctly) raised if the length of the array and the value list mismatch. (Thanks to Caleth's comment for pointing this out!) Note that copying builtin arrays is impossible in C++; that's why we have to pass the array to the function.
namespace detail {
template <typename T, std::size_t N, std::size_t... Ints, typename... Args>
void assign_helper(T (&arr)[N], std::index_sequence<Ints...>, Args&&... args)
{
((arr[Ints] = args), ...);
}
}
template <typename T, std::size_t N, typename... Args>
void assign(T (&arr)[N], Args&&... args)
{
return detail::assign_helper(arr, std::make_index_sequence<N>{}, std::forward<Args>(args)...);
}
And to use it:
int arr[3] = {10, 20, 30};
assign(arr, 40, 50, 60);
Now arr
consists of 40, 50, 60
.
live demo
Arrays can be initialized with what's called an initialization list.
Well, no.
A class can be initialized with an initialization list they must have a constructor that take an std::initializer_list
.
Example:
vector( std::initializer_list<T> init, const Allocator& alloc = Allocator());
Array are no class, so they can't have a constructor. But they can be initialized with aggregate initialization:
An aggregate is one of the following types:
- array type
- ...
And As L.F. said : They cannot be copied :
Assignment
Objects of array type cannot be modified as a whole: even though they are lvalues (e.g. an address of array can be taken), they cannot appear on the left hand side of an assignment operator
Source: https://en.cppreference.com/w/cpp/language/array
That why the {}
syntaxe works for initialization and not for assignment, because it does not mean the same thing.